SQL syntax is used to manage and manipulate data in relational databases. SQL commands are written in plain text and are used to query, insert, update, and delete data. Here is the basic structure of SQL commands:
SELECT column1, column2, ... FROM table_name WHERE condition;
Most SQL commands start with a keyword like SELECT
, INSERT
, UPDATE
, or DELETE
. Let’s dive into some basic examples.
The SELECT
statement is used to fetch data from a database. Here’s the syntax:
SELECT column1, column2 FROM table_name;
For example, to select the "name" and "age" columns from a table called "students", use the following query:
SELECT name, age FROM students;
The INSERT INTO
statement is used to add new rows to a table. Here’s the syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
For example, to insert a new student into the "students" table:
INSERT INTO students (name, age) VALUES ('Shaista', 23);
The UPDATE
statement is used to modify existing records in a table. Here’s the syntax:
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
For example, to update the age of a student named "Khan Noorain":
UPDATE students SET age = 23 WHERE name = 'Khan Noorain';
The DELETE
statement is used to delete existing records from a table. Here’s the syntax:
DELETE FROM table_name WHERE condition;
For example, to delete a student named "Anam Fatema":
DELETE FROM students WHERE name = 'Anam Fatema';
When writing SQL queries, it's important to follow a specific order of commands: