Open In App

SQL SELECT Query

Last Updated : 11 Aug, 2025
Comments
Improve
Suggest changes
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 the 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: The columns you want to retrieve.
  • table_name: The 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 int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
       (2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
       (3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
       (4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
       (5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');

Output:

Customer-table

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

Example1

Example 2: Select All Columns

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

Query:

 SELECT * FROM Customer;

Output

Example2

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:

Example3

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, Count orders per customer.

Query:

SELECT Customer_id, COUNT(*) AS order_count
FROM Orders
GROUP BY Customer_id;

Output:

Example4

Example 5: SELECT Statement with HAVING Clause

Use HAVING to filter results after grouping. Consider the following database for fetching departments with total salary above 50,000. Use WHERE for row-level filtering, HAVING for group-level filtering.

Example5

Query:

SELECT Department, sum(Salary) as Salary
FROM employee
GROUP BY department
HAVING SUM(Salary) >= 50000;  

Output:

Example5

Example 6: SELECT Statement with ORDER BY clause in SQL

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:

Example6

Common SELECT Use Cases

GoalTechnique
Fetch unique valuesSELECT DISTINCT column FROM table;
Limit result rowsSELECT * FROM table LIMIT 10; (MySQL/PostgreSQL)
Aliases for claritySELECT CustomerName AS Name FROM Customer;
Join tablesSELECT a.*, b.* FROM TableA a JOIN TableB b ON a.id = b.id;

SELECT Query in SQL
Article Tags :

Similar Reads