SQL Examples

A collection of practical SQL query examples to help you understand how to interact with relational databases.


Basic SELECT Query

The `SELECT` statement is the most common command in SQL. It is used to fetch data from a database. You can specify which columns to retrieve, from which table, and apply various conditions.

Combining `SELECT` with `WHERE` and `ORDER BY` clauses allows for powerful and precise data retrieval.

Live Example

query.sql
SELECT
  ProductID,
  ProductName,
  UnitPrice
FROM Products
WHERE UnitPrice > 50
ORDER BY UnitPrice DESC;

-- This query retrieves products with a unit price greater than 50,
-- and sorts them from highest to lowest price.
Result

// This would display a table of products with a price over 50,

// ordered from most expensive to least.

Key Concepts

Key clauses of a SELECT query:

ClauseDescription
SELECTSpecifies the columns to retrieve (e.g., ProductID, ProductName).
FROMIndicates the table to retrieve the data from (e.g., Products).
WHEREFilters records based on a specific condition (e.g., UnitPrice > 50).
ORDER BYSorts the result set based on a specified column (e.g., UnitPrice DESC).