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.

2. SELECT All Columns

SELECT * FROM student;

3. SELECT Specific Columns

SELECT RollNo, Name FROM student;

4. WHERE Clause

The WHERE clause is used to filter records based on a condition.

SELECT * FROM student
WHERE Marks > 80;

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;

6. DISTINCT Keyword

The DISTINCT keyword removes duplicate values from the result set.

SELECT DISTINCT CourseID FROM student;

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

  1. What is SELECT statement?
  2. Explain WHERE clause with example.
  3. Difference between ASC and DESC.
  4. What is DISTINCT keyword?
  5. 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