SELECT Queries
The SELECT statement is used to retrieve data from database tables.
It is the most frequently used SQL command and forms the foundation
of data querying in DBMS.
1. What is SELECT Statement?
The SELECT statement is used to fetch data from one or more tables in a database.
- Retrieves data
- Does not modify data
- Part of DQL (Data Query Language)
2. SELECT All Columns
SELECT * FROM student;
- * means all columns
- Displays entire table data
3. SELECT Specific Columns
SELECT RollNo, Name FROM student;
- Fetches selected columns only
- Improves performance
4. WHERE Clause
The WHERE clause is used to filter records based on a condition.
SELECT * FROM student WHERE Marks > 80;
- Filters rows
- Works with operators
5. ORDER BY Clause
The ORDER BY clause sorts the result set in ascending or descending order.
SELECT * FROM student ORDER BY Marks ASC;
SELECT * FROM student ORDER BY Marks DESC;
- ASC → Ascending (default)
- DESC → Descending
6. DISTINCT Keyword
The DISTINCT keyword removes duplicate values from the result set.
SELECT DISTINCT CourseID FROM student;
- Returns unique values
- Used for analysis
7. Combined Example
SELECT DISTINCT Name, Marks FROM student WHERE Marks >= 75 ORDER BY Marks DESC;
8. Comparison of Clauses
Clause Purpose ------------ ------------------------------------ SELECT Retrieves data WHERE Filters records ORDER BY Sorts records DISTINCT Removes duplicates
Practice Questions
- What is SELECT statement?
- Explain WHERE clause with example.
- Difference between ASC and DESC.
- What is DISTINCT keyword?
- Write a query using SELECT, WHERE, and ORDER BY.
Practice Task
Write SQL queries to:
✔ Display all students
✔ Display students with marks > 70
✔ Sort students by marks
✔ Display unique course IDs