SQL SELECT Statement | Complete Guide Part 1

SQL SELECT Statement

The SELECT Statement is one of the most important and frequently used commands in SQL. While INSERT, UPDATE, and DELETE modify data, the SELECT statement is primarily used to retrieve information from database tables. Almost every database application depends on SELECT queries to display records, generate reports, analyze data, and support decision-making processes.

Whenever users search for products on an e-commerce website, view student records in a college management system, check bank transactions, or generate employee reports, SQL SELECT queries work behind the scenes to fetch the required data.

Because retrieving data is a core database operation, understanding the SELECT statement is essential for students, developers, database administrators, and data analysts.


What is the SELECT Statement?

The SELECT statement is a Data Query Language (DQL) command used to retrieve data from one or more database tables. It allows users to view complete tables, specific columns, filtered records, sorted data, grouped results, and much more.

The data returned by a SELECT query is known as a result set.

A SELECT statement does not change the stored data. It only retrieves information for viewing and analysis.


Why is SELECT Important?

The SELECT statement serves as the foundation of data retrieval in SQL databases.


Basic Syntax of SELECT

The simplest syntax of a SELECT query is:

SELECT column_name
FROM table_name;

To retrieve multiple columns:

SELECT column1, column2, column3
FROM table_name;

To retrieve all columns:

SELECT *
FROM table_name;

Creating a Sample Table

Consider the following Student table:

CREATE TABLE Student (
StudentID INT,
StudentName VARCHAR(100),
Course VARCHAR(50),
Age INT
);

Sample Data:

StudentID StudentName Course Age
101 Rahul BCA 20
102 Priya B.Tech 21
103 Amit MCA 22

Selecting All Columns

The asterisk (*) symbol is used to retrieve all columns from a table.

SELECT *
FROM Student;

Output:

StudentID StudentName Course Age
101 Rahul BCA 20
102 Priya B.Tech 21
103 Amit MCA 22

This method is convenient during testing but should be used carefully in large databases because unnecessary columns may affect performance.


Selecting Specific Columns

Instead of retrieving every column, specific columns can be selected.

SELECT StudentName, Course
FROM Student;

Output:

StudentName Course
Rahul BCA
Priya B.Tech
Amit MCA

Selecting only required columns improves query performance and reduces unnecessary data transfer.


Selecting a Single Column

A query can retrieve information from only one column.

SELECT StudentName
FROM Student;

Output:

StudentName
Rahul
Priya
Amit

Using Column Aliases

Aliases provide temporary names to columns in query results.

SELECT StudentName AS Name
FROM Student;

Output:

Name
Rahul
Priya
Amit

Aliases improve readability and create user-friendly report headings.


Using Multiple Aliases

SELECT
StudentID AS ID,
StudentName AS Name,
Course AS Program
FROM Student;

The output displays customized column names.


Retrieving Constant Values

SELECT can also display constant values.

SELECT 'Welcome to CSE Gyan';

Output:

Welcome to CSE Gyan

This technique is occasionally used in testing and demonstrations.


Performing Calculations with SELECT

SQL can perform calculations directly within SELECT statements.

SELECT 100 + 50;

Output:

150

Arithmetic operations such as addition, subtraction, multiplication, and division can be performed.


DISTINCT Keyword

Sometimes a column contains duplicate values. The DISTINCT keyword removes duplicates and returns only unique values.

Example table:

Course
BCA
BCA
MCA
B.Tech

Query:

SELECT DISTINCT Course
FROM Student;

Output:

Course
BCA
MCA
B.Tech

Why DISTINCT is Useful?


Selecting Data from Employee Table

Consider the following Employee table:

EmployeeID EmployeeName Department
1 Amit HR
2 Neha IT
3 Rohit Finance

Query:

SELECT EmployeeName, Department
FROM Employee;

Only required information is displayed.


Real-World Example: College Management System

A college administrator wants to view student names and courses.

SELECT StudentName, Course
FROM Student;

The result can be displayed in a student dashboard.


Real-World Example: Banking System

Bank managers frequently retrieve customer information.

SELECT CustomerName, AccountNumber
FROM Customer;

The query displays customer account details.


Real-World Example: E-Commerce Website

Online stores display product information using SELECT queries.

SELECT ProductName, Price
FROM Products;

Customers see product names and prices on the website.


Using the WHERE Clause with SELECT

The WHERE clause is used to filter records and retrieve only those rows that satisfy a specified condition. Instead of displaying every record in a table, WHERE helps users focus on relevant information.

Syntax

SELECT column_name
FROM table_name
WHERE condition;

Example

SELECT *
FROM Student
WHERE Age > 20;

Only students whose age is greater than 20 will be displayed.


Comparison Operators in WHERE Clause

Operator Description
= Equal To
!= or <> Not Equal To
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less Than or Equal To

Using Logical Operators

Logical operators combine multiple conditions.

AND Operator

SELECT *
FROM Student
WHERE Course = 'BCA'
AND Age > 18;

OR Operator

SELECT *
FROM Student
WHERE Course = 'BCA'
OR Course = 'MCA';

NOT Operator

SELECT *
FROM Student
WHERE NOT Course = 'BCA';

ORDER BY Clause

The ORDER BY clause is used to sort query results in ascending or descending order.

Ascending Order

SELECT *
FROM Student
ORDER BY StudentName ASC;

Descending Order

SELECT *
FROM Student
ORDER BY StudentName DESC;

Sorting improves data presentation and readability.


Sorting by Multiple Columns

SELECT *
FROM Employee
ORDER BY Department ASC, Salary DESC;

Records are first sorted by department and then by salary within each department.


Aggregate Functions in SELECT

Aggregate functions perform calculations on multiple rows and return a single result.

Function Description
COUNT() Counts records
SUM() Calculates total
AVG() Calculates average
MIN() Returns smallest value
MAX() Returns largest value

COUNT Function

SELECT COUNT(*)
FROM Student;

Returns the total number of records in the Student table.


SUM Function

SELECT SUM(Salary)
FROM Employee;

Calculates the total salary of all employees.


AVG Function

SELECT AVG(Marks)
FROM Student;

Calculates the average marks of students.


MIN Function

SELECT MIN(Price)
FROM Product;

Returns the lowest product price.


MAX Function

SELECT MAX(Price)
FROM Product;

Returns the highest product price.


GROUP BY Clause

GROUP BY is used to organize rows into groups based on one or more columns.

Example

SELECT Course, COUNT(*)
FROM Student
GROUP BY Course;

The query counts students in each course.


Sample Output

Course Total Students
BCA 25
MCA 18
B.Tech 32

HAVING Clause

HAVING filters grouped records after aggregation.

SELECT Course, COUNT(*)
FROM Student
GROUP BY Course
HAVING COUNT(*) > 20;

Only courses having more than 20 students are displayed.


Using SELECT with Joins

Joins combine data from multiple related tables.

Student Table

StudentID StudentName
1 Rahul

Course Table

StudentID Course
1 BCA

INNER JOIN Example

SELECT Student.StudentName,
Course.Course

FROM Student

INNER JOIN Course
ON Student.StudentID = Course.StudentID;

The query combines student names with their course information.


SELECT with Subqueries

A subquery is a query written inside another query.

Example

SELECT StudentName
FROM Student

WHERE Marks >
(
SELECT AVG(Marks)
FROM Student
);

Displays students whose marks are above the average.


Using LIMIT with SELECT

LIMIT restricts the number of returned rows.

SELECT *
FROM Student
LIMIT 5;

Only the first five records are displayed.


Using TOP Clause (SQL Server)

SELECT TOP 5 *
FROM Student;

Displays the first five records.


Using SELECT with LIKE

LIKE searches for patterns in text.

Example

SELECT *
FROM Student
WHERE StudentName LIKE 'R%';

Returns names starting with the letter R.


Using SELECT with BETWEEN

SELECT *
FROM Student
WHERE Age BETWEEN 18 AND 22;

Returns students whose age falls within the specified range.


Using SELECT with IN Operator

SELECT *
FROM Student
WHERE Course IN ('BCA','MCA');

Displays students enrolled in either BCA or MCA.


Performance Optimization Tips


Real-World Example: College Management System

Administrators retrieve student records using SELECT queries.

SELECT StudentName, Course
FROM Student;

The result is displayed in the student portal.


Real-World Example: Banking Application

Bank employees retrieve account details.

SELECT CustomerName,
AccountNumber,
Balance

FROM Customer;

This information is displayed in customer service applications.


Real-World Example: E-Commerce Website

Online stores use SELECT queries to display products.

SELECT ProductName,
Price

FROM Products;

Customers can browse products and prices.


Advantages of SELECT Statement


Limitations of SELECT Statement


Best Practices for SELECT Queries


SQL SELECT Statement Interview Questions and Answers

1. What is the purpose of the SELECT statement?

SELECT retrieves data from one or more database tables.

2. Which SQL category does SELECT belong to?

Data Query Language (DQL).

3. What does SELECT * mean?

It retrieves all columns from a table.

4. What is the result of a SELECT query called?

A Result Set.

5. What is DISTINCT used for?

It removes duplicate values.

6. What is the purpose of WHERE?

To filter records based on conditions.

7. What is ORDER BY?

It sorts query results.

8. What is GROUP BY?

It groups rows for aggregation.

9. What is HAVING?

It filters grouped data.

10. Difference between WHERE and HAVING?

WHERE filters rows before grouping, HAVING filters groups after aggregation.

11. What does COUNT(*) do?

Counts all rows in a table.

12. What is AVG()?

Calculates average values.

13. What is SUM()?

Calculates totals.

14. What is a JOIN?

Combines related data from multiple tables.

15. What is a Subquery?

A query inside another query.

16. What is LIMIT used for?

Restricts the number of returned rows.

17. What is LIKE used for?

Pattern matching in text values.

18. What is BETWEEN?

Filters values within a specified range.

19. What is IN operator?

Checks multiple possible values.

20. Why should SELECT * be avoided in large systems?

It retrieves unnecessary data and may reduce performance.


Complete Practical Example

SELECT StudentName,
Course,
Age

FROM Student

WHERE Age > 18

ORDER BY StudentName ASC;

This query retrieves student names, courses, and ages for students older than 18 years and displays them in alphabetical order.


Conclusion

The SQL SELECT Statement is the backbone of database querying and data retrieval. It enables users to access information, generate reports, analyze records, and support business operations. From basic column selection to advanced filtering, grouping, joins, and subqueries, SELECT provides powerful capabilities for working with relational databases.

Mastering SELECT statements is essential for SQL interviews, academic learning, database development, data analysis, and real-world software applications. A strong understanding of SELECT queries allows developers to retrieve accurate information efficiently while maintaining high database performance.

← Previous: INSERT INTO Next: WHERE Clause →
Home Visit Our YouTube Channel