⬅ Previous Topic
DELETE Statement in SQLNext Topic ⮕
SQL WHERE Clause⬅ Previous Topic
DELETE Statement in SQLNext Topic ⮕
SQL WHERE ClauseOnce your data is in the database, the next step is to extract knowledge from it. The SELECT
statement is your go-to tool — your window into the table. Whether you want a list of students in Class 10A or the top scorers in Maths, it all begins here.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Let’s use a simple students
table as our base:
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune');
This retrieves everything — every row, every column.
SELECT * FROM students;
roll_no | name | class | age | city
--------+---------------+-------+-----+--------
1 | Aarav Sharma | 10A | 15 | Delhi
2 | Diya Iyer | 9B | 14 | Chennai
3 | Rohit Menon | 10A | 15 | Kochi
4 | Sneha Patil | 8C | 13 | Pune
Need just names and cities? Narrow the focus:
SELECT name, city FROM students;
name | city
---------------+---------
Aarav Sharma | Delhi
Diya Iyer | Chennai
Rohit Menon | Kochi
Sneha Patil | Pune
Filter your results. Show only Class 10A students:
SELECT * FROM students
WHERE class = '10A';
roll_no | name | class | age | city
--------+---------------+-------+-----+--------
1 | Aarav Sharma | 10A | 15 | Delhi
3 | Rohit Menon | 10A | 15 | Kochi
Use logical operators like AND and OR:
SELECT name FROM students
WHERE class = '10A' AND city = 'Kochi';
name
----------
Rohit Menon
Sort the results by age, descending:
SELECT name, age FROM students
ORDER BY age DESC;
name | age
---------------+-----
Aarav Sharma | 15
Rohit Menon | 15
Diya Iyer | 14
Sneha Patil | 13
Find unique class values:
SELECT DISTINCT class FROM students;
class
-------
10A
9B
8C
Use AS
to rename columns for better readability:
SELECT name AS student_name, city AS hometown
FROM students;
You can even perform basic arithmetic inside SELECT:
SELECT name, age, age + 1 AS next_year_age
FROM students;
name | age | next_year_age
---------------+-----+---------------
Aarav Sharma | 15 | 16
Diya Iyer | 14 | 15
Rohit Menon | 15 | 16
Sneha Patil | 13 | 14
The SELECT
statement is your querying powerhouse — whether you're scanning the full student list, targeting a specific class, or organizing by age. Mastering SELECT unlocks the full potential of SQL as a query language.
Now that you can retrieve data with SELECT, let’s move to SQL WHERE Clause in detail — your next tool for pinpoint precision in filtering records.
SELECT name FROM students WHERE marks > 90;
⬅ Previous Topic
DELETE Statement in SQLNext Topic ⮕
SQL WHERE ClauseYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.