Open In App

SQL SELECT Query

Last Updated : 25 Aug, 2025
Suggest changes
Share
Like Article
Like
Report

SQL SELECT is used to retrieve data from one or more tables, either all records or specific results based on conditions. It returns output in a tabular format of rows and columns.

  • Extracts data from tables.
  • Targets specific or all columns (*).
  • Supports filtering, sorting, grouping, and joins.
  • Results are stored in a result set.

Syntax

SELECT column1,column2.... FROM table_name;

Parameters:

  • 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

CustomerIDCustomerNameLastNameCountryAgePhone
1LiamSmithUSA23xxxxxxxxxx
2SophiaMillerUSA21xxxxxxxxxx
3AkiraTanakaJapan24xxxxxxxxxx
4CarlosHernandezUSA21xxxxxxxxxx
5IsabellaRossiItaly22xxxxxxxxxx

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

CustomerNameLastName
LiamSmith
SophiaMiller
AkiraTanaka
CarlosHernandez
IsabellaRossi

Example 2: Select All Columns

In this example, we will fetch all the fields from table Customer:

Query:

SELECT * FROM Customer;

Output

CustomerIDCustomerNameLastNameCountryAgePhone
1LiamSmithUSA23xxxxxxxxxx
2SophiaMillerUSA21xxxxxxxxxx
3AkiraTanakaJapan24xxxxxxxxxx
4CarlosHernandezUSA21xxxxxxxxxx
5IsabellaRossiItaly22xxxxxxxxxx

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

CustomerName
Sophia
Carlos

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

Countrycustomer_count
USA3
Japan1
Italy1

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

Country
USA
Japan
Italy

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

Countrycustomer_count
USA3

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

CustomerIDCustomerNameLastNameCountryAgePhone
3AkiraTanakaJapan24xxxxxxxxxx
1LiamSmithUSA23xxxxxxxxxx
5IsabellaRossiItaly22xxxxxxxxxx
2SophiaMillerUSA21xxxxxxxxxx
4CarlosHernandezUSA21xxxxxxxxxx

Article Tags :

Explore