SQL Syntax

Basic SQL Syntax

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:

General Syntax:
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.

SQL SELECT Statement

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;

SQL INSERT INTO Statement

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);

SQL UPDATE Statement

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';

SQL DELETE Statement

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';

SQL Query Order

When writing SQL queries, it's important to follow a specific order of commands:

  1. SELECT – columns to retrieve
  2. FROM – the table to retrieve data from
  3. WHERE – conditions to filter data (optional)
  4. GROUP BY – group data (optional)
  5. ORDER BY – sort results (optional)