Overview of SQL Syntax and Structure


Table of Contents

  1. Introduction
  2. SQL Syntax: The Basics
  3. SQL Keywords and Case Sensitivity
  4. The Structure of a SQL Statement
  5. Understanding Clauses in SQL
  6. Writing Your First SQL Statement
  7. The SELECT Statement
  8. Using WHERE for Filtering
  9. ORDER BY for Sorting Results
  10. INSERT INTO: Adding New Records
  11. UPDATE: Modifying Existing Data
  12. DELETE: Removing Records
  13. Creating Tables with CREATE TABLE
  14. Altering Tables with ALTER TABLE
  15. Dropping Tables with DROP TABLE
  16. SQL Comments
  17. Common Mistakes in SQL Syntax
  18. Formatting and Readability Best Practices
  19. Example: Full CRUD Cycle in SQL
  20. Summary and Next Steps

1. Introduction

Structured Query Language (SQL) is a standardized language used to communicate with relational databases. Before diving into advanced queries or optimization techniques, it’s essential to understand the syntax and structure of SQL.

This module gives you a hands-on overview of SQL’s syntax and statement structure so you can confidently begin working with relational data.


2. SQL Syntax: The Basics

SQL statements are made up of:

  • Keywords (e.g., SELECT, FROM, WHERE)
  • Identifiers (e.g., table names, column names)
  • Expressions (e.g., age > 30)
  • Literals (e.g., ‘India’, 100)

Example:

sqlCopyEditSELECT name, age FROM users WHERE age > 30;

3. SQL Keywords and Case Sensitivity

SQL keywords are not case-sensitive, meaning the following are equivalent:

sqlCopyEditSELECT * FROM users;
select * from users;
SeLeCt * FrOm users;

However, for best practice and readability, it’s common to write:

  • Keywords in uppercase (SELECT, WHERE)
  • Table and column names in lowercase or snake_case

4. The Structure of a SQL Statement

A typical SQL statement follows this logical pattern:

sqlCopyEditSELECT columns
FROM table_name
WHERE condition
GROUP BY column
HAVING condition
ORDER BY column
LIMIT number;

Not all clauses are mandatory. The SELECT, FROM, and optional WHERE are the most commonly used.


5. Understanding Clauses in SQL

Here’s a breakdown of the most common SQL clauses:

ClausePurpose
SELECTChoose which columns to retrieve
FROMSpecify the table(s) to query from
WHEREFilter records based on a condition
GROUP BYAggregate data by specified columns
HAVINGFilter grouped records
ORDER BYSort the output
LIMITLimit the number of rows returned

6. Writing Your First SQL Statement

Example:

sqlCopyEditSELECT name, email
FROM customers
WHERE country = 'India'
ORDER BY name ASC;

This retrieves all customers from India and sorts them by name in ascending order.


7. The SELECT Statement

SELECT is used to retrieve data from a database.

sqlCopyEditSELECT * FROM products;
SELECT name, price FROM products;

Use * to select all columns or specify column names explicitly.


8. Using WHERE for Filtering

WHERE is used to apply conditions:

sqlCopyEditSELECT * FROM orders
WHERE status = 'pending' AND total > 500;

Operators you can use include: =, !=, <, >, <=, >=, BETWEEN, IN, LIKE, IS NULL


9. ORDER BY for Sorting Results

sqlCopyEditSELECT * FROM users
ORDER BY created_at DESC;

ASC for ascending (default), DESC for descending order.


10. INSERT INTO: Adding New Records

sqlCopyEditINSERT INTO users (name, email, age)
VALUES ('Anjali', '[email protected]', 24);

You can insert multiple records at once:

sqlCopyEditINSERT INTO users (name, age) 
VALUES ('Ravi', 22), ('Neha', 30);

11. UPDATE: Modifying Existing Data

sqlCopyEditUPDATE users
SET age = 26
WHERE name = 'Anjali';

Always use WHERE to avoid updating all rows unintentionally.


12. DELETE: Removing Records

sqlCopyEditDELETE FROM users
WHERE age < 18;

Use caution: without WHERE, all rows will be deleted.


13. Creating Tables with CREATE TABLE

sqlCopyEditCREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE,
    age INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Defines a table and its columns, data types, and constraints.


14. Altering Tables with ALTER TABLE

To add a column:

sqlCopyEditALTER TABLE users ADD phone_number VARCHAR(15);

To remove a column:

sqlCopyEditALTER TABLE users DROP COLUMN phone_number;

15. Dropping Tables with DROP TABLE

sqlCopyEditDROP TABLE users;

Use carefully—this removes the entire table and all its data.


16. SQL Comments

You can write comments in your SQL code using:

  • -- for single-line comments
  • /* ... */ for multi-line comments

Example:

sqlCopyEdit-- This is a comment
SELECT * FROM users; /* Get all users */

17. Common Mistakes in SQL Syntax

  • Missing semicolon (;)
  • Using incorrect column or table names
  • Forgetting WHERE in UPDATE or DELETE
  • Not matching value types (e.g., using string quotes around numbers)

18. Formatting and Readability Best Practices

  • Uppercase SQL keywords (SELECT, FROM, WHERE)
  • One clause per line for readability
  • Use indentation for nested queries
  • Avoid hard-coding sensitive data

Example:

sqlCopyEditSELECT id, name
FROM employees
WHERE department = 'HR'
ORDER BY name ASC;

19. Example: Full CRUD Cycle in SQL

sqlCopyEdit-- CREATE
CREATE TABLE tasks (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255),
    status VARCHAR(50)
);

-- INSERT
INSERT INTO tasks (title, status)
VALUES ('Complete SQL module', 'pending');

-- READ
SELECT * FROM tasks;

-- UPDATE
UPDATE tasks SET status = 'completed' WHERE id = 1;

-- DELETE
DELETE FROM tasks WHERE id = 1;

20. Summary and Next Steps

In this module, you learned the foundational elements of SQL syntax and structure. From writing basic queries to understanding how to create, read, update, and delete records, you’ve laid the groundwork for deeper SQL learning.