Ch 6 · SQL Data Import and Export · SQL
Topic 6 of 8 in SQL — Query a Real Database — 4 lessons.
Loading and Exporting Data
Load data from CSV files and export query results. Instead of typing thousands of INSERT statements, you can load a whole CSV file in one go with LOAD DATA INFILE, which is much faster. SELECT INTO OUTFILE writes query results out to a file, and INSERT ... SELECT copies rows from one table into another. The first two commands are specific to MySQL and read or write files on the database server, so they cannot run in your browser. INSERT ... SELECT is standard SQL and works here.
Loading and Exporting Data in Code
These commands move data in bulk. The first block is MySQL-only and runs on a database server, not in your browser: it loads a CSV file into employees, splitting fields at commas and using IGNORE 1 LINES to skip the header row, then exports Engineering staff to a file with INTO OUTFILE. Notice how the field and line settings match the file's layout. The second block runs here: it creates an archived_employees table and copies inactive employees into it with INSERT ... SELECT. SQLite stores true and false as 1 and 0, so is_active = 0 means "not active".
Practice: Loading and Exporting Data
Here you write the three bulk commands yourself: importing a CSV file, exporting Engineering employees to a file and archiving inactive staff. The import and export are MySQL-only, so write them out but expect them to fail if you run them in the browser; the archive step runs here. A correct answer skips the CSV header row, describes how fields are separated and quoted, and copies only rows where the employee is no longer active. If MySQL refuses to read or write a file, the hint about permissions will help.
Solution & Challenge: Loading and Exporting Data
Check your commands against the worked solution for importing, exporting and archiving. The first block is MySQL-only; the second runs in your browser. The challenge then joins these steps into a small data pipeline: load raw data, find the invalid rows, fix them with UPDATE, export the cleaned result and summarise it. A good answer loads every row, clearly identifies the bad data and writes out a clean file at the end.