SQL SELECT Statement
The SELECT
statement is used to fetch data from a database. It is one of the most commonly used SQL commands.
Key Topics
1. Basic SELECT Statement
The basic syntax of a SELECT
statement is:
SELECT column1, column2 FROM table_name WHERE condition;
Code Explanation: This statement retrieves column1
and column2
from table_name
where a specific condition is met.
2. Selecting All Columns
You can select all columns from a table using the asterisk (*
) wildcard.
Example: Selecting All Columns
SELECT * FROM Employees;
Output:
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
3. Selecting Specific Columns
For better performance and readability, it's recommended to select only the columns you need.
Example: Selecting Specific Columns
SELECT FirstName, LastName FROM Employees;
Output:
FirstName | LastName |
---|---|
Karthick AG | Rajan |
Durai | Kumar |
Vijay | Muthu |
Tamil | Selvan |
Naveen | Anand |
Rudra | Kannan |
Banumathi | Ramesh |
Senthil | Raj |
Rohini | Suresh |
Nila | Prakash |
Dhanalakshmi | Gopal |
Riya | Meena |
4. Using Aliases
Aliases are used to give a table or a column a temporary name, which can make your query results more readable.
Example: Using Column Aliases
SELECT FirstName AS 'First Name', LastName AS 'Last Name' FROM Employees;
Output:
First Name | Last Name |
---|---|
Karthick AG | Rajan |
Durai | Kumar |
Vijay | Muthu |
Tamil | Selvan |
Naveen | Anand |
Rudra | Kannan |
Banumathi | Ramesh |
Senthil | Raj |
Rohini | Suresh |
Nila | Prakash |
Dhanalakshmi | Gopal |
Riya | Meena |
5. Sample Data
To demonstrate the use of the SQL SELECT statement, let's create a sample table named Employees
with the following data:
Creating the Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE,
Salary DECIMAL(10, 2),
City VARCHAR(50)
);
INSERT INTO Employees (EmployeeID, FirstName, LastName, BirthDate, Salary, City) VALUES
(1, 'Karthick AG', 'Rajan', '1980-05-15', 75000.00, 'Chennai'),
(2, 'Durai', 'Kumar', '1975-08-22', 68000.00, 'Coimbatore'),
(3, 'Vijay', 'Muthu', '1990-12-10', 82000.00, 'Madurai'),
(4, 'Tamil', 'Selvan', '1985-03-30', 72000.00, 'Tiruchirappalli'),
(5, 'Naveen', 'Anand', '1992-07-18', 69000.00, 'Salem'),
(6, 'Rudra', 'Kannan', '1988-11-05', 80000.00, 'Erode'),
(7, 'Banumathi', 'Ramesh', '1979-09-25', 71000.00, 'Vellore'),
(8, 'Senthil', 'Raj', '1991-02-14', 67000.00, 'Thanjavur'),
(9, 'Rohini', 'Suresh', '1983-04-19', 73000.00, 'Thiruvananthapuram'),
(10, 'Nila', 'Prakash', '1993-06-07', 65000.00, 'Trichy'),
(11, 'Dhanalakshmi', 'Gopal', '1987-10-21', 78000.00, 'Kanyakumari'),
(12, 'Riya', 'Meena', '1994-01-12', 64000.00, 'Vellore');
Employees Table Data
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
6. Sample Queries
Example 1: SELECT with WHERE Clause
Retrieve the first and last names of employees who earn more than 70,000.
SELECT FirstName, LastName
FROM Employees
WHERE Salary > 70000;
Output:
FirstName | LastName |
---|---|
Karthick AG | Rajan |
Vijay | Muthu |
Rudra | Kannan |
Dhanalakshmi | Gopal |
Code Explanation: This query selects the FirstName
and LastName
of employees whose Salary
is greater than 70,000.
Example 2: SELECT with ORDER BY Clause
Retrieve all employee details ordered by their salary in descending order.
SELECT *
FROM Employees
ORDER BY Salary DESC;
Output:
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
Example 3: SELECT with GROUP BY and HAVING Clauses
Find the number of employees in each city with salaries above 65,000.
SELECT City, COUNT(*) AS EmployeeCount
FROM Employees
WHERE Salary > 65000
GROUP BY City
HAVING COUNT(*) > 1;
Output:
City | EmployeeCount |
---|---|
Vellore | 2 |
Chennai | 1 |
Coimbatore | 1 |
Madurai | 1 |
Tiruchirappalli | 1 |
Salem | 1 |
Erode | 1 |
Thiruvananthapuram | 1 |
Trichy | 1 |
Kanyakumari | 1 |
Code Explanation: This query selects the City
and the count of employees in each city where the Salary
is greater than 65,000. It groups the results by City
and only includes cities with more than one employee meeting the salary condition.
6. Sample Data
To demonstrate the use of the SQL SELECT statement, let's create a sample table named Employees
with the following data:
Creating the Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE,
Salary DECIMAL(10, 2),
City VARCHAR(50)
);
INSERT INTO Employees (EmployeeID, FirstName, LastName, BirthDate, Salary, City) VALUES
(1, 'Karthick AG', 'Rajan', '1980-05-15', 75000.00, 'Chennai'),
(2, 'Durai', 'Kumar', '1975-08-22', 68000.00, 'Coimbatore'),
(3, 'Vijay', 'Muthu', '1990-12-10', 82000.00, 'Madurai'),
(4, 'Tamil', 'Selvan', '1985-03-30', 72000.00, 'Tiruchirappalli'),
(5, 'Naveen', 'Anand', '1992-07-18', 69000.00, 'Salem'),
(6, 'Rudra', 'Kannan', '1988-11-05', 80000.00, 'Erode'),
(7, 'Banumathi', 'Ramesh', '1979-09-25', 71000.00, 'Vellore'),
(8, 'Senthil', 'Raj', '1991-02-14', 67000.00, 'Thanjavur'),
(9, 'Rohini', 'Suresh', '1983-04-19', 73000.00, 'Thiruvananthapuram'),
(10, 'Nila', 'Prakash', '1993-06-07', 65000.00, 'Trichy'),
(11, 'Dhanalakshmi', 'Gopal', '1987-10-21', 78000.00, 'Kanyakumari'),
(12, 'Riya', 'Meena', '1994-01-12', 64000.00, 'Vellore');
Employees Table Data
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
7. Sample Queries
Example 1: SELECT with WHERE Clause
Retrieve the first and last names of employees who earn more than 70,000.
SELECT FirstName, LastName
FROM Employees
WHERE Salary > 70000;
Output:
FirstName | LastName |
---|---|
Karthick AG | Rajan |
Vijay | Muthu |
Rudra | Kannan |
Dhanalakshmi | Gopal |
Code Explanation: This query selects the FirstName
and LastName
of employees whose Salary
is greater than 70,000.
Example 2: SELECT with ORDER BY Clause
Retrieve all employee details ordered by their salary in descending order.
SELECT *
FROM Employees
ORDER BY Salary DESC;
Output:
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
Code Explanation: This query selects all columns from the Employees
table and orders the results by Salary
in descending order.
Example 3: SELECT with GROUP BY and HAVING Clauses
Find the number of employees in each city with salaries above 65,000.
SELECT City, COUNT(*) AS EmployeeCount
FROM Employees
WHERE Salary > 65000
GROUP BY City
HAVING COUNT(*) > 1;
Output:
City | EmployeeCount |
---|---|
Vellore | 2 |
Code Explanation: This query selects the City
and the count of employees in each city where the Salary
is greater than 65,000. It groups the results by City
and only includes cities with more than one employee meeting the salary condition.
5. Sample Data
To demonstrate the use of the SQL SELECT statement, let's create a sample table named Employees
with the following data:
Creating the Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE,
Salary DECIMAL(10, 2),
City VARCHAR(50)
);
INSERT INTO Employees (EmployeeID, FirstName, LastName, BirthDate, Salary, City) VALUES
(1, 'Karthick AG', 'Rajan', '1980-05-15', 75000.00, 'Chennai'),
(2, 'Durai', 'Kumar', '1975-08-22', 68000.00, 'Coimbatore'),
(3, 'Vijay', 'Muthu', '1990-12-10', 82000.00, 'Madurai'),
(4, 'Tamil', 'Selvan', '1985-03-30', 72000.00, 'Tiruchirappalli'),
(5, 'Naveen', 'Anand', '1992-07-18', 69000.00, 'Salem'),
(6, 'Rudra', 'Kannan', '1988-11-05', 80000.00, 'Erode'),
(7, 'Banumathi', 'Ramesh', '1979-09-25', 71000.00, 'Vellore'),
(8, 'Senthil', 'Raj', '1991-02-14', 67000.00, 'Thanjavur'),
(9, 'Rohini', 'Suresh', '1983-04-19', 73000.00, 'Thiruvananthapuram'),
(10, 'Nila', 'Prakash', '1993-06-07', 65000.00, 'Trichy'),
(11, 'Dhanalakshmi', 'Gopal', '1987-10-21', 78000.00, 'Kanyakumari'),
(12, 'Riya', 'Meena', '1994-01-12', 64000.00, 'Vellore');
Employees Table Data
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
7. Sample Queries
Example 1: SELECT with WHERE Clause
Retrieve the first and last names of employees who earn more than 70,000.
SELECT FirstName, LastName
FROM Employees
WHERE Salary > 70000;
Output:
FirstName | LastName |
---|---|
Karthick AG | Rajan |
Vijay | Muthu |
Rudra | Kannan |
Dhanalakshmi | Gopal |
Code Explanation: This query selects the FirstName
and LastName
of employees whose Salary
is greater than 70,000.
Example 2: SELECT with ORDER BY Clause
Retrieve all employee details ordered by their salary in descending order.
SELECT *
FROM Employees
ORDER BY Salary DESC;
Output:
EmployeeID | FirstName | LastName | BirthDate | Salary | City |
---|---|---|---|---|---|
3 | Vijay | Muthu | 1990-12-10 | 82000.00 | Madurai |
6 | Rudra | Kannan | 1988-11-05 | 80000.00 | Erode |
11 | Dhanalakshmi | Gopal | 1987-10-21 | 78000.00 | Kanyakumari |
1 | Karthick AG | Rajan | 1980-05-15 | 75000.00 | Chennai |
4 | Tamil | Selvan | 1985-03-30 | 72000.00 | Tiruchirappalli |
7 | Banumathi | Ramesh | 1979-09-25 | 71000.00 | Vellore |
5 | Naveen | Anand | 1992-07-18 | 69000.00 | Salem |
9 | Rohini | Suresh | 1983-04-19 | 73000.00 | Thiruvananthapuram |
2 | Durai | Kumar | 1975-08-22 | 68000.00 | Coimbatore |
8 | Senthil | Raj | 1991-02-14 | 67000.00 | Thanjavur |
10 | Nila | Prakash | 1993-06-07 | 65000.00 | Trichy |
12 | Riya | Meena | 1994-01-12 | 64000.00 | Vellore |
Code Explanation: This query selects all columns from the Employees
table and orders the results by Salary
in descending order.
Example 3: SELECT with GROUP BY and HAVING Clauses
Find the number of employees in each city with salaries above 65,000.
SELECT City, COUNT(*) AS EmployeeCount
FROM Employees
WHERE Salary > 65000
GROUP BY City
HAVING COUNT(*) > 1;
Output:
City | EmployeeCount |
---|---|
Vellore | 2 |
Code Explanation: This query selects the City
and the count of employees in each city where the Salary
is greater than 65,000. It groups the results by City
and only includes cities with more than one employee meeting the salary condition.
8. Best Practices
- Use uppercase for SQL keywords for better readability.
- Properly format and indent your SQL code.
- Comment your code to explain complex queries.
- Specify only the columns you need.
- Use aliases to improve readability.
- Avoid using
SELECT *
in production code.
Key Takeaways
- The
SELECT
statement is used to retrieve data from a database. - Selecting specific columns improves performance.
- Aliases make query results easier to understand.
- Sample data helps in understanding how queries work.
- Proper use of clauses like WHERE, ORDER BY, GROUP BY, and HAVING enhance query functionality.