SELECT Statement
Retrieving Data from SQL Tables

Introduction

Once 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.

Basic Syntax of SELECT

SELECT column1, column2, ...
FROM table_name
WHERE condition;

1. Sample Table – Students

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');

2. SELECT All Columns

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

3. SELECT Specific Columns

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

4. SELECT with WHERE Clause

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

5. SELECT with Multiple Conditions

Use logical operators like AND and OR:

SELECT name FROM students
WHERE class = '10A' AND city = 'Kochi';
name
----------
Rohit Menon

6. SELECT with ORDER BY

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

7. SELECT with DISTINCT

Find unique class values:

SELECT DISTINCT class FROM students;
class
-------
10A
9B
8C

8. SELECT with Aliases

Use AS to rename columns for better readability:

SELECT name AS student_name, city AS hometown
FROM students;

9. SELECT with Calculations

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

Best Practices

  • Use SELECT * cautiously — avoid it in production queries.
  • Filter your queries using WHERE to reduce load and noise.
  • Use aliases for clarity in reports or views.

Summary

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.

What’s Next?

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.

QUIZ

Question 1:Which SQL command is used to retrieve all records from the `students` table?

Question 2:In SQL, the SELECT statement can be used without a FROM clause.

Question 3:Which of the following are valid SELECT queries for the `students` table?

Question 4:What will be the output of this SQL statement?
SELECT name FROM students WHERE marks > 90;

Question 5:The SELECT statement can be used with aggregate functions like COUNT, AVG, and SUM.

Question 6:Which of these SQL features can be combined with the SELECT statement?