SQL SELECT Statement
The SELECT
statement is the fundamental command in SQL for retrieving data from a database. It allows you to specify which columns and rows you want to see from one or more tables.
Syntax Overview
The basic syntax for a SELECT
statement is:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Selecting Columns
You can select specific columns by listing their names after the SELECT
keyword, separated by commas. To select all columns, use the asterisk (*
).
Example: Select specific columns
SELECT CustomerName, City, Country
FROM Customers;
Example: Select all columns
SELECT *
FROM Products;
Selecting from Tables
The FROM
clause specifies the table(s) from which to retrieve data. You can select data from multiple tables by listing them in the FROM
clause, typically in conjunction with JOIN
operations (discussed later).
Column and Table Aliases
Aliases provide temporary, more readable names for columns or tables. They are useful for simplifying complex queries or when column names are not descriptive.
Example: Column alias
SELECT CustomerName AS Name, City AS Location
FROM Customers;
Example: Table alias
SELECT c.CustomerName, o.OrderID
FROM Customers AS c, Orders AS o
WHERE c.CustomerID = o.CustomerID;
DISTINCT Keyword
The DISTINCT
keyword is used to return only unique values in a specified column or set of columns. It eliminates duplicate rows from the result set.
Example: Get unique country names
SELECT DISTINCT Country
FROM Customers;
WHERE Clause
The WHERE
clause is used to filter records. It specifies a condition that must be met for a record to be included in the result set. Various comparison operators (=
, <
, >
, <>
, <=
, >=
) and logical operators (AND
, OR
, NOT
) can be used.
Example: Find customers in Mexico
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Mexico';
Example: Find customers in USA or Canada
SELECT CustomerName, Country
FROM Customers
WHERE Country = 'USA' OR Country = 'Canada';
Example: Find customers not in Germany
SELECT CustomerName, Country
FROM Customers
WHERE NOT Country = 'Germany';
ORDER BY Clause
The ORDER BY
clause is used to sort the result set in ascending (ASC
- default) or descending (DESC
) order based on one or more columns.
Example: Sort customers by country alphabetically
SELECT CustomerName, City, Country
FROM Customers
ORDER BY Country ASC;
Example: Sort customers by country descending, then city ascending
SELECT CustomerName, City, Country
FROM Customers
ORDER BY Country DESC, City ASC;
GROUP BY Clause
The GROUP BY
clause groups rows that have the same values in specified columns into summary rows, like "find the number of customers in each country". It is often used with aggregate functions (COUNT()
, MAX()
, MIN()
, SUM()
, AVG()
).
Example: Count customers in each country
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
HAVING Clause
The HAVING
clause is used to filter groups based on a specified condition. It is similar to the WHERE
clause, but it operates on grouped data, typically after the GROUP BY
clause.
Example: Find countries with more than 5 customers
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
LIMIT/TOP Clause
The LIMIT
(MySQL, PostgreSQL) or TOP
(SQL Server, MS Access) clause restricts the number of rows returned by the query. This is useful for pagination or retrieving only the first few records.
Example: Get the first 10 customers (MySQL/PostgreSQL)
SELECT CustomerName, ContactName
FROM Customers
LIMIT 10;
Example: Get the first 10 customers (SQL Server/MS Access)
SELECT TOP 10 CustomerName, ContactName
FROM Customers;
JOIN Operations
JOIN
clauses are used to combine rows from two or more tables based on a related column between them. Common types include INNER JOIN
, LEFT JOIN
, RIGHT JOIN
, and FULL OUTER JOIN
. (See the SQL JOINs section for detailed information.)
Mastering the SELECT
statement is crucial for effective data retrieval and manipulation in SQL. Practice these clauses to build powerful queries.