Inserting Data
Inserting Data
Definition: The INSERT INTO statement is used to add new records (rows) to an existing table. Once you have defined your table structure, this command allows you to fill it with actual information.
Why: Beginner SQL lessons introduce insertion immediately after table creation. This hands-on approach allows learners to populate their database so they can practice searching and filtering real data in later lessons.
Syntax
To insert data, you must specify the table name, the columns you are filling, and the corresponding values in the exact same order.
INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3);
Example: Single Row
This command adds one student record to the students table:
INSERT INTO students (id, name, age, city) VALUES (1, 'Ravi', 20, 'Bengaluru');
Example: Multiple Rows
You can add multiple records at once by separating each set of values with a comma. This is much faster than running individual commands for every row.
INSERT INTO students (id, name, age, city) VALUES (2, 'Anu', 19, 'Mysuru'), (3, 'Kiran', 21, 'Hassan');
Key Notes
- Value Formatting: Numeric values (like
idandage) are written as-is, while text values (likenameandcity) must be enclosed in single quotes (' '). - Order Precision: The order of values must perfectly match the order of the columns you listed in the first half of the statement. If you swap them, the database will return an error or store the data in the wrong fields.
- Skipping Columns: If a column allows
NULLvalues or has an "Auto Increment" setting (common for IDs), you can omit it from yourINSERTstatement, and the database will handle it automatically.
🏋️ Test Yourself With Exercises
Take our quiz on Inserting Data to test your knowledge.
Browse Quizzes »