Think of SQL syntax as the grammar and rules of SQL. Just like how sentences in English need proper structure (subject + verb + object), SQL commands need to follow a specific order for the database to understand what you're trying to say.

Basic Structure of an SQL Query

An SQL query typically has three main parts:

  1. Command: What action do you want to perform? (e.g., SELECT, INSERT, UPDATE, DELETE)

  2. Target: What data or table are you referring to? (e.g., FROM table_name)

  3. Conditions (Optional): Any filters or criteria? (e.g., WHERE condition).

Here’s a basic example:

SELECT column1, column2 
FROM table_name 
WHERE condition;

Let’s break it down:

  • SELECT: The action. We’re fetching data here.

  • column1, column2: The target columns we want.

  • FROM table_name: Specifies which table to fetch the data from.

  • WHERE condition: Adds a filter to narrow down the results.

Important SQL Commands with Syntax Examples

1. SELECT: Retrieve Data

This is the most used command to fetch data from a table.

SELECT * 
FROM employees;
  • * means “all columns.”
  • This query will return all the data in the employees table.

2. INSERT INTO: Add Data

Use this command to add new records to a table.

INSERT INTO employees (name, age, department) 
VALUES ('Subham', 30, 'IT');
  • Here, we’re inserting data for name, age, and department columns in the employees table.

3. UPDATE: Modify Existing Data

If you need to update existing records, use UPDATE.

UPDATE employees 
SET age = 31 
WHERE name = 'Subham';
  • This updates Subham’s age to 31 in the employees table.

4. DELETE: Remove Data

When you need to delete records, this command comes into play.

DELETE FROM employees 
WHERE name = 'Subham';
  • This removes the record where the name is Subham.

5. CREATE TABLE: Make a New Table

To create a new table, use the CREATE TABLE command.

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    department VARCHAR(50)
);
  • This creates an employees table with columns for id, name, age, and department.

6. DROP TABLE: Delete a Table

Be careful with this one—it completely removes the table and its data.

DROP TABLE employees;

Some Key Points to Remember

  • Case Sensitivity: SQL keywords like SELECT and FROM are not case-sensitive (select works just as well), but table and column names might be, depending on the database.

  • Semicolon: Always end your SQL statements with a semicolon (;), especially when running multiple queries.

  • Whitespace: SQL ignores extra spaces, so use indentation to make your queries readable.

Tips for Writing Good SQL Queries

  1. Start with the Basics: Practice SELECT and WHERE before moving to more advanced commands.

  2. Use Comments: Add comments to your queries for clarity.

    -- This fetches all employees in the IT department
    SELECT * 
    FROM employees 
    WHERE department = 'IT';
  3. Test with Small Data: Before running queries on large datasets, test them on a small sample to avoid accidental data loss.