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:

EmployeeIDFirstNameLastNameBirthDateSalaryCity
1Karthick AGRajan1980-05-1575000.00Chennai
2DuraiKumar1975-08-2268000.00Coimbatore
3VijayMuthu1990-12-1082000.00Madurai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
5NaveenAnand1992-07-1869000.00Salem
6RudraKannan1988-11-0580000.00Erode
7BanumathiRamesh1979-09-2571000.00Vellore
8SenthilRaj1991-02-1467000.00Thanjavur
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
10NilaPrakash1993-06-0765000.00Trichy
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
12RiyaMeena1994-01-1264000.00Vellore

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:

FirstNameLastName
Karthick AGRajan
DuraiKumar
VijayMuthu
TamilSelvan
NaveenAnand
RudraKannan
BanumathiRamesh
SenthilRaj
RohiniSuresh
NilaPrakash
DhanalakshmiGopal
RiyaMeena

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 NameLast Name
Karthick AGRajan
DuraiKumar
VijayMuthu
TamilSelvan
NaveenAnand
RudraKannan
BanumathiRamesh
SenthilRaj
RohiniSuresh
NilaPrakash
DhanalakshmiGopal
RiyaMeena

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
1Karthick AGRajan1980-05-1575000.00Chennai
2DuraiKumar1975-08-2268000.00Coimbatore
3VijayMuthu1990-12-1082000.00Madurai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
5NaveenAnand1992-07-1869000.00Salem
6RudraKannan1988-11-0580000.00Erode
7BanumathiRamesh1979-09-2571000.00Vellore
8SenthilRaj1991-02-1467000.00Thanjavur
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
10NilaPrakash1993-06-0765000.00Trichy
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
12RiyaMeena1994-01-1264000.00Vellore

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:

FirstNameLastName
Karthick AGRajan
VijayMuthu
RudraKannan
DhanalakshmiGopal

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:

EmployeeIDFirstNameLastNameBirthDateSalaryCity
3VijayMuthu1990-12-1082000.00Madurai
6RudraKannan1988-11-0580000.00Erode
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
1Karthick AGRajan1980-05-1575000.00Chennai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
7BanumathiRamesh1979-09-2571000.00Vellore
5NaveenAnand1992-07-1869000.00Salem
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
2DuraiKumar1975-08-2268000.00Coimbatore
8SenthilRaj1991-02-1467000.00Thanjavur
10NilaPrakash1993-06-0765000.00Trichy
12RiyaMeena1994-01-1264000.00Vellore

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:

CityEmployeeCount
Vellore2
Chennai1
Coimbatore1
Madurai1
Tiruchirappalli1
Salem1
Erode1
Thiruvananthapuram1
Trichy1
Kanyakumari1

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
1Karthick AGRajan1980-05-1575000.00Chennai
2DuraiKumar1975-08-2268000.00Coimbatore
3VijayMuthu1990-12-1082000.00Madurai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
5NaveenAnand1992-07-1869000.00Salem
6RudraKannan1988-11-0580000.00Erode
7BanumathiRamesh1979-09-2571000.00Vellore
8SenthilRaj1991-02-1467000.00Thanjavur
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
10NilaPrakash1993-06-0765000.00Trichy
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
12RiyaMeena1994-01-1264000.00Vellore

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:

FirstNameLastName
Karthick AGRajan
VijayMuthu
RudraKannan
DhanalakshmiGopal

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:

EmployeeIDFirstNameLastNameBirthDateSalaryCity
3VijayMuthu1990-12-1082000.00Madurai
6RudraKannan1988-11-0580000.00Erode
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
1Karthick AGRajan1980-05-1575000.00Chennai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
7BanumathiRamesh1979-09-2571000.00Vellore
5NaveenAnand1992-07-1869000.00Salem
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
2DuraiKumar1975-08-2268000.00Coimbatore
8SenthilRaj1991-02-1467000.00Thanjavur
10NilaPrakash1993-06-0765000.00Trichy
12RiyaMeena1994-01-1264000.00Vellore

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:

CityEmployeeCount
Vellore2

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
1Karthick AGRajan1980-05-1575000.00Chennai
2DuraiKumar1975-08-2268000.00Coimbatore
3VijayMuthu1990-12-1082000.00Madurai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
5NaveenAnand1992-07-1869000.00Salem
6RudraKannan1988-11-0580000.00Erode
7BanumathiRamesh1979-09-2571000.00Vellore
8SenthilRaj1991-02-1467000.00Thanjavur
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
10NilaPrakash1993-06-0765000.00Trichy
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
12RiyaMeena1994-01-1264000.00Vellore

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:

FirstNameLastName
Karthick AGRajan
VijayMuthu
RudraKannan
DhanalakshmiGopal

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:

EmployeeIDFirstNameLastNameBirthDateSalaryCity
3VijayMuthu1990-12-1082000.00Madurai
6RudraKannan1988-11-0580000.00Erode
11DhanalakshmiGopal1987-10-2178000.00Kanyakumari
1Karthick AGRajan1980-05-1575000.00Chennai
4TamilSelvan1985-03-3072000.00Tiruchirappalli
7BanumathiRamesh1979-09-2571000.00Vellore
5NaveenAnand1992-07-1869000.00Salem
9RohiniSuresh1983-04-1973000.00Thiruvananthapuram
2DuraiKumar1975-08-2268000.00Coimbatore
8SenthilRaj1991-02-1467000.00Thanjavur
10NilaPrakash1993-06-0765000.00Trichy
12RiyaMeena1994-01-1264000.00Vellore

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:

CityEmployeeCount
Vellore2

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.