Lesson 15 of 60 – SELECT
25%

SELECT in SQL

The SELECT statement is one of the most important SQL commands. It is used to retrieve or read data from one or more tables.

Note: SELECT does not normally change the data in a table. It is mainly used to retrieve and display information.

1. What is SELECT?

The SELECT statement is used to retrieve records from a database table.

SELECT * FROM Students;

This query displays all columns and all records from the Students table.

2. Basic SELECT Syntax

The basic syntax is:

SELECT column_name
FROM table_name;

You specify the columns that you want to retrieve followed by the table name.

3. SELECT All Columns

The asterisk * means all columns.

SELECT * FROM Students;

Suppose the table contains:

ID Name Age Course
1 Rahul 20 Python
2 Priya 21 ADCA

The query will return all four columns.

4. SELECT Specific Columns

You can retrieve only the columns that you need.

SELECT name, age
FROM Students;

This query returns only the name and age columns.

5. SELECT One Column

SELECT name
FROM Students;

This retrieves only the student names.

6. SELECT Multiple Columns

SELECT id, name, course
FROM Students;

You can specify multiple columns separated by commas.

7. SELECT with WHERE

The WHERE clause is used to filter records.

SELECT *
FROM Students
WHERE age = 20;

Only students whose age is 20 will be returned.

8. SELECT Specific Data

SELECT name, course
FROM Students
WHERE age = 20;

This retrieves the name and course of students whose age is 20.

9. SELECT with Comparison Operator

You can use comparison operators such as >, <, >=, <=, and =.

SELECT *
FROM Students
WHERE age > 20;

This returns students whose age is greater than 20.

10. SELECT Text Data

Text values are normally enclosed in single quotes.

SELECT *
FROM Students
WHERE course = 'Python';

This returns students enrolled in Python.

11. SELECT with AND

The AND operator allows multiple conditions.

SELECT *
FROM Students
WHERE age > 18
AND course = 'Python';

Both conditions must be true.

12. SELECT with OR

The OR operator returns records when at least one condition is true.

SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';

13. SELECT with ORDER BY

The ORDER BY clause sorts the returned records.

SELECT *
FROM Students
ORDER BY name;

By default, sorting is generally ascending.

14. ORDER BY DESC

Use DESC to sort data in descending order.

SELECT *
FROM Students
ORDER BY age DESC;

15. ORDER BY ASC

Use ASC for ascending order.

SELECT *
FROM Students
ORDER BY age ASC;

ASC is the default sorting direction.

16. SELECT with LIMIT

In MySQL, LIMIT can restrict the number of returned records.

SELECT *
FROM Students
LIMIT 5;

This returns up to five records.

17. SELECT DISTINCT

The DISTINCT keyword removes duplicate values from the result.

SELECT DISTINCT course
FROM Students;

This returns each course only once.

18. SELECT with COUNT

The COUNT() function can count records.

SELECT COUNT(*)
FROM Students;

This returns the total number of rows in the table.

19. SELECT with SUM

The SUM() function calculates the total of numeric values.

SELECT SUM(fee)
FROM Students;

This can be useful for calculating the total fee amount.

20. SELECT with AVG

The AVG() function calculates the average value.

SELECT AVG(age)
FROM Students;

21. SELECT with MIN

The MIN() function returns the smallest value.

SELECT MIN(age)
FROM Students;

22. SELECT with MAX

The MAX() function returns the largest value.

SELECT MAX(age)
FROM Students;

23. SELECT with LIKE

The LIKE operator is used to search for a pattern.

SELECT *
FROM Students
WHERE name LIKE 'A%';

The % wildcard means zero or more characters. This example finds names starting with A.

24. SELECT with BETWEEN

The BETWEEN operator checks whether a value is within a specified range.

SELECT *
FROM Students
WHERE age BETWEEN 18 AND 25;

This returns students whose age is between 18 and 25, including the boundary values.

25. SELECT with IN

The IN operator allows you to specify multiple possible values.

SELECT *
FROM Students
WHERE course IN ('Python', 'Java', 'PHP');

This returns students whose course matches one of the listed values.

26. SELECT with NULL

To find NULL values, use IS NULL.

SELECT *
FROM Students
WHERE mobile IS NULL;

To find values that are not NULL:

SELECT *
FROM Students
WHERE mobile IS NOT NULL;

27. SELECT from Multiple Tables

SELECT can also retrieve data from multiple tables using techniques such as JOIN.

SELECT Students.name, Courses.course_name
FROM Students
INNER JOIN Courses
ON Students.course_id = Courses.course_id;

JOINs will be covered in detail in later SQL lessons.

28. SELECT with Column Alias

The AS keyword can provide a temporary name for a column in the result.

SELECT name AS Student_Name
FROM Students;

The result will display the column using the alias Student_Name.

29. Practical Student Queries

Display all students:

SELECT * FROM Students;

Display only names:

SELECT name FROM Students;

Display Python students:

SELECT *
FROM Students
WHERE course = 'Python';

Display students older than 20:

SELECT *
FROM Students
WHERE age > 20;

30. Complete SELECT Example

SELECT name, course, fee
FROM Students
WHERE fee > 5000
AND course = 'Python'
ORDER BY fee DESC
LIMIT 5;

This query:

  • Selects the name, course, and fee columns.
  • Filters records where fee is greater than 5000.
  • Filters records where the course is Python.
  • Sorts the result by fee from highest to lowest.
  • Returns up to five records.

📌 Key Points

  • SELECT is used to retrieve data from a table.
  • SELECT * retrieves all columns.
  • You can select one or multiple columns.
  • WHERE filters records.
  • ORDER BY sorts the result.
  • LIMIT restricts the number of returned rows in MySQL.
  • DISTINCT removes duplicate values from the result.
  • COUNT(), SUM(), AVG(), MIN(), and MAX() are aggregate functions.
  • LIKE searches for patterns.
  • BETWEEN searches within a range.
  • IN checks against multiple possible values.
  • IS NULL is used to find NULL values.

🧠 Quick Quiz

Question: Which SQL statement is used to retrieve data from a table?