What is a SELECT Query?
Imagine you have a giant notebook — a register of every student in your school, with columns for Roll Number, Name, Class, and Marks. If someone asks "Who scored above 90%?", you don't read the whole notebook aloud. You flip through, pick only the rows that match, and maybe even write down just the names.
That's exactly what a SELECT query does in a database. It's your way of asking the database: "Show me specific information from this table, under these conditions."
The Intuition First
A table is just rows and columns. A SELECT query lets you:
- Choose which columns you want to see (like picking only Name and Marks, ignoring Roll Number)
- Choose which rows you want (like only students in Class 12, or only those with Marks > 90)
- Optionally sort the results, group them, or rename the columns for clarity
You are not changing the original table — you are just querying it, like asking a question and getting a printed answer.
The Precise Statement
The simplest form of a SELECT query is:
SELECT column1, column2, ...
FROM table_name;
If you want every column, use * (a wildcard meaning "all columns"):
SELECT * FROM Students;
That gives you the entire table, unchanged.
Adding Conditions: The WHERE Clause
Most real queries need a filter. You add a WHERE clause:
SELECT Name, Marks
FROM Students
WHERE Marks > 90;
This says: "From the Students table, show me only the Name and Marks columns, but only for rows where Marks is greater than 90."
The WHERE clause is evaluated row by row. For each row, the condition is checked — if true, that row appears in the result; if false, it is skipped.
Sorting Results: ORDER BY
To arrange the output in a specific order:
SELECT Name, Marks
FROM Students
WHERE Class = '12A'
ORDER BY Marks DESC;
DESC means descending (highest first). ASC (ascending) is the default if you don't specify.
Renaming Columns: Aliases
Sometimes the column name in the table is cryptic (like mrks or stu_name). You can rename it in the output using AS:
SELECT Name AS StudentName, Marks AS Percentage
FROM Students;
The table still has the original column names — only the result sheet shows your chosen labels.
A Complete Example
Suppose you have a table called Employees:
| EmpID | Name | Department | Salary |
|---|
| 101 | Ravi | Sales | 45000 |
| 102 | Priya | IT | 62000 |
| 103 | Aman | Sales | 38000 |
| 104 | Neha | IT | 71000 |