Academic Block

SQL Syntax Get Certified

What is SQL Syntax?

SQL syntax is the set of rules used to write SQL statements. SQL commands are made up of keywords, table names, column names, operators, expressions, and other clauses.

A SQL statement normally ends with a semicolon (;), which separates one statement from another.

Basic SQL Statement

What this code does: Creates a temporary table of students and selects their name and score.

WITH students(id, name, score) AS (
  VALUES
    (1, 'Aman', 85),
    (2, 'Neha', 92),
    (3, 'Rahul', 78)
)
SELECT name, score
FROM students;

SQL Keywords

What this code does: Filters products that cost more than 500 using the WHERE clause.

WITH products(id, name, price) AS (
  VALUES
    (1, 'Keyboard', 800),
    (2, 'Mouse', 400),
    (3, 'Monitor', 1500)
)
SELECT name, price
FROM products
WHERE price > 500;

SQL Statements End with a Semicolon

What this code does: Shows a basic SELECT statement that ends with a semicolon.

WITH numbers(value) AS (
  VALUES
    (10),
    (20),
    (30)
)
SELECT value
FROM numbers;

Selecting Specific Columns

What this code does: Selects only the name, department, and salary columns from the employees table.

WITH employees(id, name, department, salary) AS (
  VALUES
    (1, 'Aarav', 'IT', 65000),
    (2, 'Meera', 'Finance', 72000),
    (3, 'Rohan', 'Sales', 58000)
)
SELECT name, department, salary
FROM employees;

Selecting All Columns

What this code does: Uses the asterisk (*) to return every column from the employees table.

WITH employees(id, name, department) AS (
  VALUES
    (1, 'Aarav', 'IT'),
    (2, 'Meera', 'Finance'),
    (3, 'Rohan', 'Sales')
)
SELECT *
FROM employees;

Using WHERE in SQL Syntax

What this code does: Filters products whose price is greater than or equal to 1000.

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500),
    ('Webcam', 1000)
)
SELECT name, price
FROM products
WHERE price >= 1000;

Using ORDER BY

What this code does: Sorts products from highest price to lowest price.

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500),
    ('Webcam', 1000)
)
SELECT name, price
FROM products
ORDER BY price DESC;

Using LIMIT

What this code does: Returns only the top 3 most expensive products.

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500),
    ('Webcam', 1000),
    ('Speaker', 2200)
)
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;

Using DISTINCT

What this code does: Returns only unique department names (removes duplicates).

WITH employees(name, department) AS (
  VALUES
    ('Aarav', 'IT'),
    ('Meera', 'Finance'),
    ('Rohan', 'IT'),
    ('Anika', 'HR'),
    ('Vikram', 'Finance')
)
SELECT DISTINCT department
FROM employees;

SQL String Values

What this code does: Finds the student whose name is exactly ‘Neha’ using a string comparison.

WITH students(name, age) AS (
  VALUES
    ('Aman', 21),
    ('Neha', 22),
    ('Rahul', 20)
)
SELECT name, age
FROM students
WHERE name = 'Neha';

SQL Comparison Operators

What this code does: Returns products that cost more than 700.

WITH products(name, price) AS (
  VALUES
    ('Mouse', 400),
    ('Keyboard', 800),
    ('Monitor', 1500),
    ('Laptop', 60000)
)
SELECT name, price
FROM products
WHERE price > 700;

Using AND

What this code does: Finds employees who work in the IT department and earn more than 60,000.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'IT', 55000),
    ('Anika', 'HR', 60000)
)
SELECT name, department, salary
FROM employees
WHERE department = 'IT'
  AND salary > 60000;

Using OR

What this code does: Returns products that belong to either the Computer or Stationery category.

WITH products(name, category) AS (
  VALUES
    ('Keyboard', 'Computer'),
    ('Mouse', 'Computer'),
    ('Notebook', 'Stationery'),
    ('Pen', 'Stationery')
)
SELECT name, category
FROM products
WHERE category = 'Computer'
   OR category = 'Stationery';

Using IN

What this code does: Selects employees who work in either the IT or Finance department.

WITH employees(name, department) AS (
  VALUES
    ('Aarav', 'IT'),
    ('Meera', 'Finance'),
    ('Rohan', 'Sales'),
    ('Anika', 'HR')
)
SELECT name, department
FROM employees
WHERE department IN ('IT', 'Finance');

Using BETWEEN

What this code does: Finds products whose price is between 500 and 1600 (inclusive).

WITH products(name, price) AS (
  VALUES
    ('Mouse', 400),
    ('Keyboard', 800),
    ('Monitor', 1500),
    ('Speaker', 2500)
)
SELECT name, price
FROM products
WHERE price BETWEEN 500 AND 1600;

Using LIKE

What this code does: Finds all student names that start with the letter “A”.

WITH students(name) AS (
  VALUES
    ('Aman'),
    ('Anika'),
    ('Rahul'),
    ('Meera')
)
SELECT name
FROM students
WHERE name LIKE 'A%';

Using IS NULL

What this code does: Finds employees who do not have a manager assigned (NULL value).

WITH employees(name, manager) AS (
  VALUES
    ('Aarav', 'Ravi'),
    ('Meera', NULL),
    ('Rohan', 'Priya')
)
SELECT name, manager
FROM employees
WHERE manager IS NULL;

Using Column Aliases

What this code does: Calculates total price (price × quantity) and gives friendly column names using AS.

WITH products(name, price, quantity) AS (
  VALUES
    ('Keyboard', 800, 2),
    ('Mouse', 400, 3)
)
SELECT name AS product_name,
       price * quantity AS total_price
FROM products;

Comments in SQL

What this code does: Demonstrates a single-line comment using --.

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500)
)
-- Select products costing more than 500
SELECT name, price
FROM products
WHERE price > 500;

Common SQL Clauses

Keyword / Clause
Purpose
Example
SELECT
Choose columns or expressions
SELECT name
FROM
Specify the data source
FROM employees
WHERE
Filter rows
WHERE salary > 60000
GROUP BY
Group rows
GROUP BY department
HAVING
Filter grouped results
HAVING COUNT(*) > 2
ORDER BY
Sort results
ORDER BY salary DESC
LIMIT
Restrict returned rows in SQLite
LIMIT 5

General SELECT Query Structure

What this code does: Demonstrates a complete SELECT query with WHERE, GROUP BY, HAVING, ORDER BY and LIMIT.

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'IT', 58000),
    ('Anika', 'HR', 62000)
)
SELECT department,
       COUNT(*) AS employee_count,
       AVG(salary) AS average_salary
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING COUNT(*) > 0
ORDER BY average_salary DESC
LIMIT 10;

Important SQL Syntax Rules

  • SQL keywords are generally written in uppercase for readability, although SQLite is case-insensitive for keywords.
  • Column and table names should be written consistently.
  • Separate multiple column names with commas.
  • Use single quotes for SQL string literals.
  • Use parentheses when needed to control the order of expressions.
  • Use a semicolon to terminate SQL statements.
  • Use comments to document complicated queries.

Best Practices

  • Write SQL keywords consistently in uppercase.
  • Use indentation for complex queries.
  • Use meaningful table and column names.
  • Use aliases when they make a query easier to understand.
  • Avoid unnecessary complexity in SQL statements.
  • Test queries with small datasets before using them on large datasets.

Understanding SQL syntax is the foundation of writing effective database queries. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.