October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Export SQL Query Results to CSV or Excel Without Losing Data

A practical guide to exporting SQL query results as CSV or Excel while avoiding broken quoting, lost leading zeros, altered numbers, and misread dates.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To export SQL results safely, choose the format your recipient needs, use the export method for your database, and control how spreadsheet software interprets the file. CSV is portable text, not a workbook with preserved cell types or formatting; Excel can change identifiers, long numbers, and dates unless you import them deliberately.

Choose CSV or an Excel workbook

Use CSV when the recipient needs a portable, plain-text table for another tool or system. Use an Excel workbook when the recipient needs spreadsheet features such as multiple sheets or formatting, or when your database client provides direct workbook output. Confirm what the selected client actually exports: CSV does not preserve formulas, formatting, multiple sheets, or explicit cell types.

As an Amazon Associate I earn from qualifying purchases.

Before exporting, establish whether the output should include a header row, how NULL and empty values should be represented, what encoding to use, and whether the recipient expects a particular delimiter or date convention. These details matter especially when a file will be loaded back into a database or another system.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I export SQL query results to CSV?

Export commands vary by database engine. Also distinguish a server-side file, written where the database server can access it, from a client-side file, written on the computer running the database client. The latter is usually what you want for a local download. Check the manual for your installed product and version before using a command.

#1 Best Overall
Sale
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
  • Easily store and access 2TB to content on the go with the Seagate Portable Drive, a USB external hard drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

PostgreSQL: write a local CSV with psql

In psql, use the client-side copy meta-command to transfer a query result through the connection and write it to a path accessible to the client:

copy (SELECT id, code, created_at FROM public.example ORDER BY id) TO 'results.csv' WITH (FORMAT CSV, HEADER, ENCODING 'UTF8')

Replace the query and path with your own. This is a psql meta-command, not a SQL statement to send as-is through another client. The path is interpreted by the client operating system; adapt its quoting and path conventions accordingly. PostgreSQL’s server-side COPY ... TO 'filename', by contrast, writes to a path accessible to the server process and may require appropriate server permissions.

Rank #2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
  • Easily store and access 5TB of content on the go with the Seagate portable drive, a USB external hard Drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

In CSV mode, PostgreSQL quotes fields that contain the delimiter, quote character, carriage return, or line feed. Its documented default distinguishes NULL from a non-NULL empty string: NULL is an unquoted empty field, while an empty string is quoted. If another application will reload the export, preserve or explicitly document the chosen NULL-marker settings so the distinction is not lost.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

MySQL 8.0: server-side export with SELECT INTO OUTFILE

MySQL 8.0 documents SELECT ... INTO OUTFILE for writing query results with configurable field terminators, optional enclosure, escaping, and line terminators. This writes from the server’s perspective. The database server must be able to write to the destination, and server filesystem and security restrictions apply; the command does not automatically download a file to your workstation. Consult the MySQL 8.0 manual and check the rules for your installation before choosing a path or permissions.

Rank #3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
  • Easily store and access 1TB to content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop. Reformatting may be required for Mac
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

If you want a desktop workflow instead, MySQL Workbench documents exporting SQL-editor result grids to formats including CSV and Excel XML. Available formats and steps depend on the installed Workbench version; use its matching documentation. Do not substitute PostgreSQL syntax for a MySQL export command, or assume their file-access rules are alike.

Other database engines and clients

For another engine, use its official export command or the export function in your installed client. Check whether it exports a query result or a whole table, where it writes the file, what permissions it needs, and how it handles delimiter, quoting, escaping, NULL, and encoding. Do not assume a command or output option exists across products or versions.

Rank #4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue
  • Easily store and access 4TB of content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Make the CSV structurally reliable

CSV is structured text, and the extension alone does not ensure correct quoting, encoding, or interpretation. A field containing a delimiter, quote, or line break needs to be quoted and escaped according to the export format. Prefer an exporter that handles these rules rather than assembling CSV by concatenating values in a query.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Agree on the delimiter and quoting rules: the recipient must parse fields containing delimiters, quotes, or line breaks correctly.
  • Set the encoding intentionally: for example, the PostgreSQL command above requests UTF-8. Confirm the recipient can read the chosen encoding.
  • Decide how NULL differs from empty text: an export/import pair can conflate them or distinguish them depending on settings. Document the representation when round-tripping matters.
  • Keep headers and column order predictable: select the needed columns explicitly and use stable names rather than relying on an unspecified column order.

Import CSV into Excel without changing values

Do not rely on double-clicking a CSV when columns contain identifiers or values that resemble numbers, dates, or scientific notation. Excel may remove leading zeros, convert long digit strings to scientific notation, retain only 15 digits of numeric precision, or interpret continuous letter-and-number strings as dates. These are documented Excel behaviors, not guarantees that every value will be changed.

Best Value
Sale
UnionSine 500GB Ultra Slim Portable External Hard Drive HDD-USB 3.0
  • [Upgraded Version] - This external hard drive features a mirrored logo stripe combined with a striped anti-slip design, and the rounded corners of the casing make it easier to grip. The stripes also have a heat dissipation function, ensuring stable and fast data transfer.
  • 【Ultra-thin and quiet】 - The motherboard adopts JMicron 578 noise-free solution, giving you a quiet working environment. Lightweight and portable size designed to fit in your pocket for easy portability.
  • 【Ultra-Fast Data Transfers】 - Pairing this external hard drive with JMicron 578 solution USB 3.0 and USB 2.0 interfaces enables blazing-fast data transfer. It boasts theoretical read speeds of up to 125MB/s and write speeds of up to 103MB/s.
  • 【Plug and Play】 - With no software to install, just plug it in and the drive is ready to use.The hard disk chip is wrapped with an aluminum anti-interference layer to increase heat dissipation and protect data.
  • 【What You Get】 - 1 x Portable Hard Drive, 1 x USB 3.0 Cable, 1 x User Manual, Gift-type shell packaging ,Three-year manufacturer's warranty and free technical support services.
  1. In Excel, choose Data > From Text/CSV and select the file.
  2. Choose Transform Data when you need to control column types before loading.
  3. Set identifier-like columns, such as product codes, account numbers, or ZIP codes, to Text. A code like 00123 is an identifier, not a quantity; setting it to Text prevents Excel from treating it as a number and stripping its leading zeros.
  4. Check the file encoding and the locale used to parse dates and numbers, then load the data.
  5. Compare representative rows with the database result. For sensitive extracts, also compare row counts and known identifiers or totals before sharing the workbook.

For an ambiguous date such as 03/04/2025, first establish whether the source means March 4 or April 3. Then choose the matching import locale and date type. Do not treat a successful load as proof that Excel interpreted every column as intended.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Respect Excel’s worksheet limits

Microsoft documents a maximum of 1,048,576 rows and 16,384 columns for Excel text import and export. The same row limit applies to what a worksheet can contain, so a larger SQL result cannot fit in a single worksheet. Power Query can handle data beyond worksheet fill capacity in some workflows, but it does not increase the worksheet’s capacity. For larger extracts, use a suitable database or data-processing destination, or intentionally split or transform the output rather than relying on one worksheet.

Choose an export method that fits the handoff

Method Where the file is written What to check
PostgreSQL psql copy Client computer Client path and permissions; CSV options such as header, encoding, and NULL representation.
PostgreSQL server-side COPY Server-accessible path Server path and permissions; this is not automatically a local download.
MySQL 8.0 SELECT ... INTO OUTFILE Server-side destination Server filesystem and security constraints; field, enclosure, escape, and line-terminator options.
MySQL Workbench result-grid export Client workflow; destination details depend on the installed version Available CSV or Excel XML options and the behavior documented for that Workbench version.

These methods are product- and version-specific. Whichever you use, validate the delivered file in the application or system that will consume it. For Excel, inspect sensitive types and locale parsing; for a system-to-system handoff, verify the agreed delimiter, quoting, encoding, column order, and NULL convention.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 1
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$129.99
Bestseller No. 2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$229.99
Bestseller No. 3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$119.80
Bestseller No. 4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$208.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.