Open In App

SQL SELECT Query

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The select query in SQL is one of the most commonly used SQL commands to retrieve data from a database. With the select command in SQL, users can access data and retrieve specific records based on various conditions, making it an essential tool for managing and analyzing data.

In this article, we’ll learn the SQL SELECT statement syntax, show you SQL query examples, and explore advanced techniques to help you use SELECT queries for data manipulation and analysis.

What is the SQL SELECT Statement?

The SELECT clause is the first clause and is one of the last clauses of the select statement that the database server evaluates. The reason for this is that before we can determine what to include in the final result set, we need to know all of the possible columns that could be included in the final result set.

The SELECT statement in SQL is used to fetch or retrieve data from a database. It allows users to access the data and retrieve specific data based on specific conditions. We can fetch either the entire table or according to some specified rules. The data returned is stored in a result table. With the SELECT clause of a SELECT command statement, we specify the columns that we want to be displayed in the query result.

Syntax:

SELECT column1,column2…. FROM table_name ;

Examples of SELECT Statement

Let’s look at some examples of the SQL SELECT statement, to understand it better. To demonstrate the examples, let’s create a table which will be used in examples:

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:

create table

Example 1: Select Specific Columns

In this example, we will fetch only CustomerName, LastName from the table Customer:

Query:

SELECT CustomerName, LastName 
FROM Customer;

Output

output table

Example 2: Select All Columns

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

Query:

 SELECT * FROM Customer;

Output

fetch all field of the table

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:

select statement with where clause example 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, Count orders per customer.

Query:

SELECT COUNT (item), Customer_id 
FROM Orders
GROUP BY order_id;

Output:

sql select statement with group by clause example output

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.

demo table

Query:

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

Output:

select statement with having clause example output

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:

select statement with order by clause in sql

Tips to Master SELECT

Goal Technique
Fetch unique values SELECT DISTINCT column FROM table;
Limit result rows SELECT * FROM table LIMIT 10; (MySQL/PostgreSQL)
Aliases for clarity SELECT CustomerName AS Name FROM Customer;
Join tables SELECT a.*, b.* FROM TableA a JOIN TableB b ON a.id = b.id;

Conclusion

The SQL SELECT statement is an essential tool for retrieving and analyzing data from relational databases. Whether we’re fetching specific columns or using advanced clauses like WHERE, GROUP BY, and ORDER BY, the SELECT query provides flexibility for efficient data retrieval. By understanding how to use the SELECT statement and combining it with various clauses, we can efficiently filter, aggregate, and sort data to meet our needs.



Next Article

Similar Reads