SQL Syntax and Queries

Understanding the foundation of relational database interaction

Introduction to SQL

SQL (Structured Query Language) is the standard language for interacting with relational databases. Developed in the 1970s at IBM, SQL has evolved into a powerful, standardized language that serves as the foundation for most relational database systems including MySQL, PostgreSQL, SQL Server, Oracle, and SQLite.

Analogy: SQL as a Library Assistant

Imagine a massive library with millions of books organized across multiple floors and sections. SQL acts like an efficient library assistant who knows exactly where everything is located. When you need information, you don't search the entire library yourself—you simply give specific instructions to this assistant using a special language (SQL), and they retrieve exactly what you need.

Want all books by a specific author? Just ask. Need information that spans multiple categories? The assistant will gather and organize it for you. Need to add new books or update information? The assistant handles that too. SQL is that specialized language that lets you communicate precisely what you want to do with your data.

Why SQL Matters

SQL Language Components

SQL is divided into several key components, each serving specific functions:

graph TD SQL[SQL Language] --> DDL[Data Definition Language] SQL --> DML[Data Manipulation Language] SQL --> DCL[Data Control Language] SQL --> TCL[Transaction Control Language] SQL --> DQL[Data Query Language] DDL --> CREATE[CREATE - Make new objects] DDL --> ALTER[ALTER - Modify objects] DDL --> DROP[DROP - Remove objects] DDL --> TRUNCATE[TRUNCATE - Empty tables] DML --> INSERT[INSERT - Add data] DML --> UPDATE[UPDATE - Modify data] DML --> DELETE[DELETE - Remove data] DCL --> GRANT[GRANT - Assign privileges] DCL --> REVOKE[REVOKE - Remove privileges] TCL --> COMMIT[COMMIT - Save changes] TCL --> ROLLBACK[ROLLBACK - Undo changes] TCL --> SAVEPOINT[SAVEPOINT - Create restore points] DQL --> SELECT[SELECT - Retrieve data] style SQL fill:#f9f,stroke:#333,stroke-width:2px style DDL fill:#fcf,stroke:#333 style DML fill:#cfc,stroke:#333 style DCL fill:#ccf,stroke:#333 style TCL fill:#ffc,stroke:#333 style DQL fill:#cff,stroke:#333

This lecture focuses primarily on DQL (SELECT statements) and some basic DML operations, as these form the foundation of SQL querying.

Basic SQL Syntax Elements

SQL Statement Structure

SQL statements typically follow a logical structure:

Basic SQL Statement Structure:


-- A basic SQL query structure
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column_name;
          

Key SQL Syntax Rules

SQL Syntax Examples:


-- This is a single-line comment
SELECT first_name, last_name  -- This selects two columns
FROM employees;

/* This is a 
   multi-line comment
   demonstrating SQL's flexibility */
SELECT 
    employee_id,
    first_name,
    last_name,
    hire_date
FROM 
    employees
WHERE 
    department_id = 50
ORDER BY 
    hire_date DESC;
          

Basic SELECT Queries

The SELECT statement is the most commonly used command in SQL, allowing you to retrieve data from one or more tables.

Simple SELECT Examples

Selecting All Columns:


-- Retrieve all columns from the customers table
SELECT * FROM customers;
          

Selecting Specific Columns:


-- Retrieve only name and email columns
SELECT customer_name, email FROM customers;
          

Selecting with a Basic Filter:


-- Retrieve customers from a specific country
SELECT customer_name, email, phone 
FROM customers
WHERE country = 'USA';
          

Column Aliases

Aliases allow you to rename columns in your result set, making output more readable or accommodating specific application requirements.

Using Column Aliases:


-- Rename columns in the result set
SELECT 
    first_name AS "First Name",
    last_name AS "Last Name",
    email AS "Contact Email"
FROM employees;

-- Shorthand without AS keyword
SELECT 
    product_name "Product",
    unit_price "Price",
    units_in_stock "Inventory"
FROM products;
          

Filtering Data with WHERE

The WHERE clause allows you to filter results based on specific conditions, returning only rows that match your criteria.

Comparison Operators

Operator Description Example
= Equal to WHERE price = 9.99
< Less than WHERE price < 20
> Greater than WHERE quantity > 100
<= Less than or equal to WHERE discount <= 15
>= Greater than or equal to WHERE salary >= 50000
<> or != Not equal to WHERE status <> 'Cancelled'

Logical Operators

Combine multiple conditions for more complex filtering:

Using AND, OR, and NOT:


-- Using AND to match multiple conditions
SELECT product_name, unit_price, category
FROM products
WHERE unit_price > 50 AND category = 'Electronics';

-- Using OR to match any of multiple conditions
SELECT first_name, last_name, hire_date
FROM employees
WHERE department = 'Sales' OR department = 'Marketing';

-- Using NOT to negate a condition
SELECT order_id, order_date, status
FROM orders
WHERE NOT status = 'Delivered';
          

Combining Multiple Operators:


-- Combining AND, OR with parentheses for clarity
SELECT product_id, product_name, unit_price, units_in_stock
FROM products
WHERE 
    (category = 'Beverages' OR category = 'Condiments')
    AND unit_price > 10
    AND units_in_stock > 0;
          

Special Operators and Conditions

BETWEEN Operator

The BETWEEN operator selects values within a given range (inclusive).

Using BETWEEN:


-- Retrieve products within a price range
SELECT product_name, unit_price
FROM products
WHERE unit_price BETWEEN 10 AND 20;

-- Equivalent to:
-- WHERE unit_price >= 10 AND unit_price <= 20

-- BETWEEN with dates
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date BETWEEN '2023-01-01' AND '2023-03-31';
          

IN Operator

The IN operator allows you to specify multiple values in a WHERE clause, acting as a shorthand for multiple OR conditions.

Using IN:


-- Find customers in specific countries
SELECT customer_name, country
FROM customers
WHERE country IN ('USA', 'Canada', 'Mexico');

-- Equivalent to:
-- WHERE country = 'USA' OR country = 'Canada' OR country = 'Mexico'

-- Using IN with subqueries (more on this later)
SELECT product_name, unit_price
FROM products
WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Beverages');
          

LIKE Operator for Pattern Matching

The LIKE operator is used for string pattern matching, using wildcards:

Using LIKE:


-- Find customers whose names start with 'A'
SELECT customer_name
FROM customers
WHERE customer_name LIKE 'A%';

-- Find products with 'coffee' anywhere in the name
SELECT product_name
FROM products
WHERE product_name LIKE '%coffee%';

-- Find employees with exactly 5-character last names
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '_____';

-- Find products with price values ending in .99
SELECT product_name, unit_price
FROM products
WHERE CAST(unit_price AS VARCHAR) LIKE '%.99';
          

NULL Values

NULL represents a missing or unknown value. You can't use regular comparison operators with NULL; instead, use IS NULL or IS NOT NULL.

Handling NULL Values:


-- Find employees without a manager
SELECT employee_id, first_name, last_name
FROM employees
WHERE manager_id IS NULL;

-- Find customers with phone numbers
SELECT customer_name, phone
FROM customers
WHERE phone IS NOT NULL;
          

Sorting Results with ORDER BY

The ORDER BY clause sorts your query results based on one or more columns, in ascending (ASC) or descending (DESC) order.

Basic Sorting:


-- Sort customers alphabetically by name (ascending is default)
SELECT customer_id, customer_name, city
FROM customers
ORDER BY customer_name;

-- Sort products by price in descending order (highest first)
SELECT product_name, unit_price
FROM products
ORDER BY unit_price DESC;
          

Multi-Column Sorting:


-- Sort employees by department, then by salary (highest within each department)
SELECT employee_id, first_name, last_name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;
          

Sorting by Column Position:


-- Sort by the second column in the SELECT list
SELECT product_name, unit_price, units_in_stock
FROM products
ORDER BY 2 DESC;  -- Sorts by unit_price descending
          

Analogy: Library Book Organization

Think of SQL sorting like organizing books in a library:

  • A simple ORDER BY author is like arranging books alphabetically by author surname
  • Using ORDER BY genre, author is like first grouping books by genre, then alphabetically by author within each genre
  • Adding DESC like in ORDER BY publication_date DESC is like arranging books with newest editions first

Just as libraries can organize books differently based on needs (by author, by subject, by popularity), SQL lets you sort data according to your specific requirements.

Limiting Results

Different database systems offer ways to limit the number of rows returned:

LIMIT in MySQL/PostgreSQL/SQLite:


-- Return only the first 10 products
SELECT product_id, product_name, unit_price
FROM products
ORDER BY unit_price DESC
LIMIT 10;
          

TOP in SQL Server:


-- Return only the first 10 products
SELECT TOP 10 product_id, product_name, unit_price
FROM products
ORDER BY unit_price DESC;
          

FETCH in ANSI SQL / Oracle 12c+:


-- Return only the first 10 products
SELECT product_id, product_name, unit_price
FROM products
ORDER BY unit_price DESC
FETCH FIRST 10 ROWS ONLY;
          

Pagination with LIMIT and OFFSET:


-- Get the second page of 10 products (rows 11-20)
SELECT product_id, product_name, unit_price
FROM products
ORDER BY product_name
LIMIT 10 OFFSET 10;
          

Using Distinct Values

The DISTINCT keyword eliminates duplicate rows from your result set.

Basic DISTINCT Usage:


-- Get a list of all unique countries where customers are located
SELECT DISTINCT country
FROM customers
ORDER BY country;

-- Get unique combination of city and country
SELECT DISTINCT city, country
FROM customers
ORDER BY country, city;
          
Original Data ID Country 1 USA 2 Canada 3 USA 4 Mexico 5 Canada SELECT DISTINCT Country Country USA Canada Mexico

Practical Activities

Activity 1: Basic SQL Queries

Using a sample database (like Northwind or Sakila), write SQL queries to:

  1. Retrieve all columns from the products table
  2. List the names and email addresses of all customers
  3. Find all products with a price greater than $50
  4. List all employees hired before 2020
  5. Find all orders placed in the last quarter of 2022

Activity 2: Filtering and Sorting

Write queries that:

  1. Find all products in the 'Electronics' category with more than 10 units in stock, sorted by price (highest first)
  2. List customers from Germany or France whose company name starts with the letter 'A'
  3. Find employees without a manager
  4. List orders with shipping date between March 1 and March 31, 2023
  5. Find products with the word "chair" in their name, regardless of case

Activity 3: Real-World Challenge

Imagine you're working for an online bookstore. Write SQL queries to:

  1. List the 10 bestselling books of 2023
  2. Find all science fiction books published in the last 5 years
  3. List authors who have published books in multiple genres
  4. Find books that are running low on inventory (less than 5 copies)
  5. List customers who have placed orders totaling more than $500

Key Takeaways

Next Steps

Our next lecture will cover CRUD operations in SQL, focusing on INSERT, UPDATE, and DELETE statements for managing data in your tables.