SQL SELECT Query
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.
SELECT Statement in SQL
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 and, optionally, which column headings we prefer to see above the result table.
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.
Syntax:
The syntax for the SELECT statement is:
SELECT column1,column2…. FROM table_name ;
SELECT Statement Example
Let’s look at some examples of the SQL SELECT statement, to understand it better.
Let’s create a table which will be used in examples:
CREATE TABLE:
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:

Example 1: Retrieve Data using SELECT Query
In this example, we will fetch CustomerName, LastName from the table Customer:
Query:
SELECT CustomerName, LastName FROM Customer;
Output:

Example 2: Fetch All Table using SELECT Statement
In this example, we will fetch all the fields from the table Customer:
Query:
SELECT * FROM Customer;
Output:

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.
Query:
SELECT CustomerName FROM Customer where Age = '21';
Output:

Example 4: SQL SELECT Statement with GROUP BY Clause
In this example, we will use SELECT statement with GROUP BY Clause
Query:
SELECT COUNT (item), Customer_id FROM Orders GROUP BY order_id;
Output:

Example 5: SELECT Statement with HAVING Clause
Consider the following database for HAVING Clause:

Query:
SELECT Department, sum(Salary) as Salary FROM employee GROUP BY department HAVING SUM(Salary) >= 50000;
Output:
Example 6: SELECT Statement with ORDER BY clause in SQL
In this example, we will use SELECT Statement with ORDER BY clause
Query:
SELECT * FROM Customer ORDER BY Age DESC;
Output:
Important Points with SQL SELECT Statement
- It is used to access records from one or more database tables and views.
- The SELECT statement retrieves selected data based on specified conditions.
- The result of a SELECT statement is stored in a result set or result table.
- The SELECT statement can be used to access specific columns or all columns from a table.
- It can be combined with clauses like WHERE, GROUP BY, HAVING, and ORDER BY for more refined data retrieval.
- The SELECT statement is versatile and allows users to fetch data based on various criteria efficiently.
Conclusion
The SQL SELECT statement is an essential tool for retrieving and analyzing data from relational databases. Whether you’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, you can efficiently filter, aggregate, and sort data to meet your needs.