SQL Query Guide

Text-to-SQL Guide for Core and Advanced SQL Queries

Generate, optimize, and understand SQL statements with clear examples for every major SQL clause.

SQL SELECT Query

The SELECT statement is used to retrieve data from one or more database tables. It is the most common SQL query and a core part of reporting, filtering, and analytics workflows.

Generate a SELECT query with AI Prompt (Text-to-SQL):
Get all customers

Generated SQL:

SELECT
  *
FROM
  customer;
SQL SELECT syntax Select specific columns:
SELECT column1, column2
FROM table_name;

Select all columns:

SELECT *
FROM table_name;
Example result set Running the query returns rows like:
customer_idfirst_namelast_nameemail
1MARYSMITHMARY.SMITH@…
2PATRICIAJOHNSONPATRICIA.JOHNSON@…
3LINDAWILLIAMSLINDA.WILLIAMS@…

SQL FROM Clause

The FROM clause tells SQL which table (or tables) to query. It appears in nearly every SELECT query and is also used with DELETE statements.

Generate a FROM query with AI Prompt (Text-to-SQL):
Get all customers

Generated SQL:

SELECT
  *
FROM
  customer;
SQL FROM syntax Basic syntax:
SELECT column1, column2
FROM table_name;

With a join:

SELECT c.customer_id, c.first_name, a.address
FROM customer AS c
JOIN address AS a ON c.address_id = a.address_id;
Example result set | customer_id | first_name | last_name | email | | :--- | :--- | :--- | :--- | | 1 | MARY | SMITH | MARY.SMITH@... | | 2 | PATRICIA | JOHNSON | PATRICIA.JOHNSON@... | | 3 | LINDA | WILLIAMS | LINDA.WILLIAMS@... |

SQL WHERE Clause

The WHERE clause is used to filter rows. It can be used in statements like SELECT, UPDATE, and DELETE to return or affect only matching records.

Generate a WHERE query with AI Prompt (Text-to-SQL):
Get customers named Smith

Generated SQL:

SELECT
  customer_id,
  first_name,
  last_name
FROM
  customer
WHERE
  last_name = 'Smith';
SQL WHERE syntax and operators Basic syntax:
SELECT column1, column2
FROM table_name
WHERE condition;

Common operators:

OperatorDescription
=Equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
<>Not equal (sometimes written as !=)
BETWEENBetween a range
LIKEPattern match
INMatches any value in a list

Example with OR:

SELECT
  customer_id,
  first_name,
  last_name
FROM
  customer
WHERE
  last_name = 'Smith' OR last_name = 'Jones';

Example with AND:

SELECT
  customer_id,
  first_name,
  last_name
FROM
  customer
WHERE
  first_name = 'Mary' AND last_name = 'Smith';
Example result set Running the query returns rows like:
customer_idfirst_namelast_name
1MARYSMITH

SQL JOIN Clause

The JOIN clause combines data from multiple tables using related columns. This is essential for analytics, reporting, and relational data lookups.

Generate a JOIN query with AI Prompt (Text-to-SQL):
Get each customer with their address

Generated SQL:

SELECT
  c.customer_id,
  c.first_name,
  c.last_name,
  a.address,
  a.district
FROM
  customer AS c
JOIN address AS a ON c.address_id = a.address_id;
SQL JOIN syntax and types Basic syntax:
SELECT column1, column2
FROM table1
JOIN table2 ON table1.column_name = table2.column_name;

Common join types:

JOIN typeDescription
INNER JOINRows that match in both tables
LEFT JOINAll rows from left table + matching rows from right
RIGHT JOINAll rows from right table + matching rows from left
FULL OUTER JOINRows that match in either table
Example result set | customer_id | first_name | last_name | address | district | | :--- | :--- | :--- | :--- | :--- | | 1 | MARY | SMITH | 1913 Hanoi Way | California | | 2 | PATRICIA | JOHNSON | 1121 Park Avenue | New York City | | 3 | LINDA | WILLIAMS | 692 Joliet Street | Attika |

SQL GROUP BY Clause

The GROUP BY clause groups rows that share the same value, often together with aggregates like COUNT, SUM, and AVG.

Generate a GROUP BY query with AI Prompt (Text-to-SQL):
Count the number of customers in each country

Generated SQL:

SELECT
  country,
  COUNT(customer_id) AS total_customers
FROM
  customer
GROUP BY
  country;
SQL GROUP BY syntax ```sql SELECT column_name, aggregate_function(column_name) FROM table_name WHERE condition GROUP BY column_name; ```

Example:

SELECT
  product_id,
  SUM(amount) AS total_sales
FROM
  sales
GROUP BY
  product_id;
Example result set | country | total_customers | | :--- | :--- | | USA | 5 | | Canada | 3 | | Mexico | 2 | | UK | 1 |

SQL ORDER BY Clause

The ORDER BY clause sorts query results in ascending (ASC) or descending (DESC) order. It improves readability and is commonly used in dashboards and reports.

Generate an ORDER BY query with AI Prompt (Text-to-SQL):
Get customers ordered by last name

Generated SQL:

SELECT
  customer_id,
  first_name,
  last_name
FROM
  customer
ORDER BY
  last_name ASC;
SQL ORDER BY syntax ```sql SELECT column1, column2 FROM table_name ORDER BY column1 ASC, column2 DESC; ```

Example:

SELECT
  customer_id,
  first_name,
  last_name,
  create_date
FROM
  customer
ORDER BY
  create_date DESC;
Example result set | customer_id | first_name | last_name | | :--- | :--- | :--- | | 2 | JOHN | DOE | | 3 | JANE | JOHNSON | | 1 | MARY | SMITH |

SQL LIMIT Clause

The LIMIT clause restricts how many rows are returned. It is useful for pagination, previews, and performance when working with large datasets.

Generate a LIMIT query with AI Prompt (Text-to-SQL):
Get the first 5 customers

Generated SQL:

SELECT
  customer_id,
  first_name,
  last_name
FROM
  customer
LIMIT 5;
SQL LIMIT syntax Basic usage:
SELECT column1, column2
FROM table_name
LIMIT number;

With offset:

SELECT column1, column2
FROM table_name
LIMIT offset, number;
Example result set | customer_id | first_name | last_name | | :--- | :--- | :--- | | 1 | MARY | SMITH | | 2 | PATRICIA | JOHNSON | | 3 | LINDA | WILLIAMS | | 4 | BARBARA | JONES | | 5 | ELIZABETH | BROWN |

SQL INSERT INTO Statement

The INSERT INTO statement adds new rows to a table. For reliable inserts, your prompt should mention the table, required fields, and the values to insert.

Generate an INSERT query with AI Prompt (Text-to-SQL):
Insert a new customer named John Doe with email john.doe@example.com

Generated SQL:

INSERT INTO customer (
  first_name,
  last_name,
  email,
  address_id,
  store_id,
  active,
  create_date
)
VALUES (
  'JOHN',
  'DOE',
  'john.doe@example.com',
  1,
  1,
  1,
  NOW()
);
SQL INSERT syntax Specify columns:
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);

Insert all columns in table order:

INSERT INTO table_name
VALUES (value1, value2, value3);
Example result set After running the query, a row like this is added:
customer_idfirst_namelast_nameemail
JOHNDOEjohn.doe@example.com

SQL UPDATE Statement

The UPDATE statement changes existing rows. In production SQL workflows, a WHERE clause is critical to avoid updating every row in a table.

Generate an UPDATE query with AI Prompt (Text-to-SQL):
Update email to mary.smith@sakilacustomer.org for customer with id 31

Generated SQL:

UPDATE customer
SET
  email = 'mary.smith@sakilacustomer.org'
WHERE
  customer_id = 31;
SQL UPDATE syntax ```sql UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition; ```

Example:

UPDATE customer
SET store_id = 2
WHERE store_id = 1;
Example result set | customer_id | first_name | last_name | email | | :--- | :--- | :--- | :--- | | 1 | MARY | SMITH | mary.smith@sakilacustomer.org |

SQL DELETE Statement

The DELETE statement removes rows from a table. Always include a WHERE clause unless you intentionally want to clear the full table.

Generate a DELETE query with AI Prompt (Text-to-SQL):
Delete customer named JANE DOE

Generated SQL:

DELETE FROM customer
WHERE first_name = 'JANE'
  AND last_name = 'DOE';
SQL DELETE syntax ```sql DELETE FROM table_name WHERE condition; ```

Example:

DELETE FROM customer
WHERE email LIKE '%@hotmail.com';
Example data before and after Before:
customer_idfirst_namelast_nameemail
1MARYSMITHMARY.SMITH@sakilacustomer.org
2JANEDOE
3JOHNDOEJOHN.DOE@hotmail.com

After deleting customers without an email:

customer_idfirst_namelast_nameemail
1MARYSMITHMARY.SMITH@sakilacustomer.org
3JOHNDOEJOHN.DOE@hotmail.com

Advanced SQL Query Examples

These advanced SQL examples show how AI can generate complex logic such as time windows, geospatial filters, ranking, and multi-table views.

Example 1: customers inactive for the last 12 months Prompt (Text-to-SQL):
Get customers who did not purchase in the last 12 months but did purchase in the previous 12 months

Generated SQL:

SELECT
  c.customer_id,
  c.first_name,
  c.last_name
FROM
  customer AS c
  JOIN payment AS p ON c.customer_id = p.customer_id
WHERE
  p.payment_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 24 MONTH) AND DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
  AND c.customer_id NOT IN (
    SELECT
      p2.customer_id
    FROM
      payment AS p2
    WHERE
      p2.payment_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 12 MONTH) AND CURDATE()
  )
GROUP BY
  c.customer_id,
  c.first_name,
  c.last_name;
Example 2: geospatial customer filtering Prompt (Text-to-SQL):
Get customers within 500m of the Eiffel Tower or Times Square using lat/lng

Generated SQL:

SELECT
  c.first_name,
  c.last_name,
  c.email
FROM
  customer AS c
  JOIN address AS a ON c.address_id = a.address_id
WHERE
  ST_Distance_Sphere(a.location, POINT(2.2945, 48.8584)) <= 500
  OR ST_Distance_Sphere(a.location, POINT(-73.9855, 40.7580)) <= 500;
Example 3: sort customers by purchase count Prompt (Text-to-SQL):
Find customers who purchased last year and sort by number of purchases

Generated SQL:

SELECT
  c.customer_id,
  c.first_name,
  c.last_name,
  COUNT(p.payment_id) AS purchase_count
FROM
  customer AS c
  JOIN payment AS p ON c.customer_id = p.customer_id
WHERE
  YEAR(p.payment_date) = YEAR(CURDATE()) - 1
GROUP BY
  c.customer_id,
  c.first_name,
  c.last_name
ORDER BY
  purchase_count DESC;
Example 4: create a reusable SQL view Prompt (Text-to-SQL):
Create a view called Customers NYC with customer, address, and rental data

Generated SQL:

CREATE VIEW `Customers NYC` AS
SELECT
  c.customer_id,
  c.first_name,
  c.last_name,
  c.email,
  a.address,
  a.address2,
  a.district,
  a.postal_code,
  a.phone,
  ci.city,
  co.country,
  r.rental_date,
  r.return_date
FROM
  customer AS c
  JOIN address AS a ON c.address_id = a.address_id
  JOIN city AS ci ON a.city_id = ci.city_id
  JOIN country AS co ON ci.country_id = co.country_id
  LEFT JOIN rental AS r ON c.customer_id = r.customer_id
WHERE
  ci.city = 'New York';

Dataset

The SQL examples are based on the video rental store database, Sakila for MySQL. (SQL queries for other databases are very similar.)