The SQL SELECT statement retrieves data from one or more tables and returns it as a tabular result set of rows and columns. You can fetch all columns with * or choose specific columns, and narrow results using WHERE filters. Queries can be ordered with ORDER BY, grouped and aggregated with GROUP BY/HAVING, and combined across tables using JOINs.
Syntax:
SELECT column1,column2.... FROM table_name;
- column1, column2: columns you want to retrieve.
- table_name: name of the table you're querying.
Examples of SELECT Statement
Let us start by creating a sample table that we will use for our examples. We will also insert some sample data to make the demonstration more practical.
CREATE TABLE Customer (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age INT(2),
Phone VARCHAR(10) );
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Liam', 'Smith', 'USA', 23, 'xxxxxxxxxx'),
(2, 'Sophia', 'Miller', 'USA', 21, 'xxxxxxxxxx'),
(3, 'Akira', 'Tanaka', 'Japan', 24, 'xxxxxxxxxx'),
(4, 'Carlos', 'Hernandez', 'USA', 21, 'xxxxxxxxxx'),
(5, 'Isabella', 'Rossi', 'Italy', 22, 'xxxxxxxxxx');
Output
| CustomerID | CustomerName | LastName | Country | Age | Phone |
|---|
| 1 | Liam | Smith | USA | 23 | xxxxxxxxxx |
| 2 | Sophia | Miller | USA | 21 | xxxxxxxxxx |
| 3 | Akira | Tanaka | Japan | 24 | xxxxxxxxxx |
| 4 | Carlos | Hernandez | USA | 21 | xxxxxxxxxx |
| 5 | Isabella | Rossi | Italy | 22 | xxxxxxxxxx |
Example 1: Select Specific Columns
In this example, we will demonstrate how to retrieve specific columns from the Customer table. Here we will fetch only CustomerName and LastName for each record.
Query:
SELECT CustomerName, LastName
FROM Customer;
Output:
| CustomerName | LastName |
|---|
| Liam | Smith |
| Sophia | Miller |
| Akira | Tanaka |
| Carlos | Hernandez |
| Isabella | Rossi |
Example 2: Select All Columns
In this example, we will fetch all the fields from table Customer:
Query:
SELECT * FROM Customer;
Output
| CustomerID | CustomerName | LastName | Country | Age | Phone |
|---|
| 1 | Liam | Smith | USA | 23 | xxxxxxxxxx |
| 2 | Sophia | Miller | USA | 21 | xxxxxxxxxx |
| 3 | Akira | Tanaka | Japan | 24 | xxxxxxxxxx |
| 4 | Carlos | Hernandez | USA | 21 | xxxxxxxxxx |
| 5 | Isabella | Rossi | Italy | 22 | xxxxxxxxxx |
Example 3: SELECT Statement with WHERE Clause
Suppose we want to see table values with specific conditions then WHERE Clause is used with select statement. In this example, filter customers who are 21 years old.
Query:
SELECT CustomerName
FROM Customer
where Age = '21';
Output
Example 4: SELECT with GROUP BY Clause
In this example, we will use SELECT statement with GROUP BY Clause to group rows and perform aggregation. Here, we will count the number of customers from each country.
Query:
SELECT Country, COUNT(*) AS customer_count
FROM Customer
GROUP BY Country;
Output
| Country | customer_count |
|---|
| USA | 3 |
|---|
| Japan | 1 |
|---|
| Italy | 1 |
|---|
Example 5: SELECT with DISTINCT Clause
In this example, we will use DISTINCT keyword to return only unique values from a column. Here, we will fetch unique countries from the Customer table.
Query:
SELECT DISTINCT Country
FROM Customer;
Output:
Example 6: SELECT Statement with HAVING Clause
The HAVING clause is used to filter results after applying GROUP BY. In this example, we will find countries that have 2 or more customers in the Customer table.
Query:
SELECT Country, COUNT(*) AS customer_count
FROM Customer
GROUP BY Country
HAVING COUNT(*) >= 2;
Output
| Country | customer_count |
|---|
| USA | 3 |
Example 7: SELECT Statement with ORDER BY clause
In this example, we will use SELECT Statement with ORDER BY clause. Here, Sort results by Age in descending order.
Query:
SELECT * FROM Customer ORDER BY Age DESC;
Output:
| CustomerID | CustomerName | LastName | Country | Age | Phone |
|---|
| 3 | Akira | Tanaka | Japan | 24 | xxxxxxxxxx |
| 1 | Liam | Smith | USA | 23 | xxxxxxxxxx |
| 5 | Isabella | Rossi | Italy | 22 | xxxxxxxxxx |
| 2 | Sophia | Miller | USA | 21 | xxxxxxxxxx |
| 4 | Carlos | Hernandez | USA | 21 | xxxxxxxxxx |
Explore
Basics
Queries & Operations
SQL Joins & Functions
Data Constraints & Aggregate Functions
Advanced SQL Topics
Database Design & Security