How to Convert a CSV File into a SQL Table (with Examples)

INTRODUCTION

When you convert a CSV file into a SQL table, the conversion follows a predictable pattern: your CSV headers become column names, and each row of data becomes a SQL INSERT statement. Once you understand this mapping, the entire process becomes much clearer. This guide walks you through it with real examples.

How CSV Headers Become Column Names
The first row of a well-formed CSV file contains column headers. 

These headers map directly to column names in the SQL CREATE TABLE statement.

CSV header row:
id,first_name,last_name,email,signup_date

Generated SQL CREATE TABLE:
CREATE TABLE users (
id INT,
first_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255),
signup_date DATE
);

The converter reads each header, assigns an appropriate data type based on the values in that column, and generates the CREATE TABLE schema.

How CSV Rows Become SQL INSERT Statements


Each data row in the CSV becomes one INSERT INTO statement in the SQL output.

CSV data rows:
1,Alice,Johnson,alice@example.com,2024-01-15
2,Bob,Smith,bob@example.com,2024-02-20

Generated INSERT statements:
INSERT INTO users (id, first_name, last_name, email, signup_date) VALUES (1, ‘Alice’, ‘Johnson’, ‘alice@example.com’, ‘2024-01-15’);
INSERT INTO users (id, first_name, last_name, email, signup_date) VALUES (2, ‘Bob’, ‘Smith’, ‘bob@example.com’, ‘2024-02-20’);

Understanding Automatic Data Type Detection
A good CSV to SQL converter will infer data types from the values in each column:

  • All digits, no decimal → INTEGER
  • Digits with decimal point → DECIMAL or FLOAT
  • Text with @ sign and dot → VARCHAR (email)
  • YYYY-MM-DD format → DATE
  • True/False, Yes/No → BOOLEAN
  • Everything else → VARCHAR(255)

You can usually override these suggestions in the converter interface before generating the SQL.

Example 1 — Converting a Products CSV
Input CSV:

product_id,product_name,price,in_stock,category
101,Wireless Mouse,29.99,true,Electronics
102,Desk Lamp,45.00,true,Office
103,Notebook,8.50,false,Stationery

Generated SQL:
CREATE TABLE products (
product_id INT,
product_name VARCHAR(255),
price DECIMAL(10,2),
in_stock BOOLEAN,
category VARCHAR(255)
);
INSERT INTO products VALUES (101, ‘Wireless Mouse’, 29.99, TRUE, ‘Electronics’);
INSERT INTO products VALUES (102, ‘Desk Lamp’, 45.00, TRUE, ‘Office’);
INSERT INTO products VALUES (103, ‘Notebook’, 8.50, FALSE, ‘Stationery’);

Example 2 — Converting a Contacts CSV
Input CSV:


contact_id,full_name,phone,company,country
1,Maria Garcia,+34612345678,TechCorp,Spain
2,Liam Brown,+447700900123,DesignLtd,UK

Generated SQL:
CREATE TABLE contacts (
contact_id INT,
full_name VARCHAR(255),
phone VARCHAR(50),
company VARCHAR(255),
country VARCHAR(100)
);
INSERT INTO contacts VALUES (1, ‘Maria Garcia’, ‘+34612345678’, ‘TechCorp’, ‘Spain’);
INSERT INTO contacts VALUES (2, ‘Liam Brown’, ‘+447700900123’, ‘DesignLtd’, ‘UK’);

Downloading Your SQL Table Script

Once the conversion is complete, download the .sql file. You can run this file in:

  • MySQL: mysql -u root -p database_name < output.sql
  • PostgreSQL: psql -d database_name -f output.sql
  • SQLite: sqlite3 mydb.db < output.sql
  • GUI tools: MySQL Workbench, pgAdmin, TablePlus, DBeaver

CONCLUSION


Converting a CSV into a SQL table follows a simple and consistent pattern. Headers become columns, rows become INSERT statements, and data types are inferred automatically. Use our free online tool to see this conversion in action with your own CSV file.

Leave a Comment

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

Scroll to Top