How to Retrieve Data in SQL with SELECT | Beginner's Complete Guide
Introduction
One of the most common tasks in SQL is retrieving data from a database. The SELECT statement allows you to query and fetch information stored in tables. It is the most powerful and widely used command in SQL for data analysis, reporting, and application development.
In this lesson, you will learn the basics of using SELECT to retrieve data from your SQL tables.
1. Basic Syntax of SELECT
The basic structure of a SELECT statement is:
SELECT column1, column2, ...
FROM table_name;
If you want to retrieve all columns from a table, you can use the * wildcard:
SELECT * FROM table_name;
2. Example: Retrieving Data
Suppose you have a table called students:
CREATE TABLE students (
id INT,
name VARCHAR(50),
age INT
);
Retrieve all columns:
SELECT * FROM students;
Retrieve specific columns:
SELECT name, age FROM students;
This will return only the name and age of each student.
3. Retrieving Specific Rows
You can combine SELECT with conditions (like WHERE) to filter rows.
Example:
SELECT * FROM students
WHERE age > 20;
This query retrieves all students whose age is greater than 20.
4. Retrieving Unique Values with DISTINCT
Sometimes, you want only unique (non-duplicate) values from a column. You can use the DISTINCT keyword.
Example:
SELECT DISTINCT age FROM students;
This will return a list of different ages without any repetitions.
5. Using Aliases (AS) for Better Readability
You can rename columns or tables temporarily using aliases.
Example:
SELECT name AS student_name, age AS student_age
FROM students;
This helps make your output more readable and professional.
6. Retrieving Data from Multiple Tables (JOIN Overview)
You can also retrieve data from multiple related tables using JOIN statements (more advanced topic).
Simple overview:
SELECT students.name, courses.course_name
FROM students
INNER JOIN enrollments ON students.id = enrollments.student_id
INNER JOIN courses ON enrollments.course_id = courses.id;
This query shows which student is enrolled in which course.
(You will learn about JOINs in detail later!)
7. ORDER BY: Sorting Retrieved Data
You can sort your query results using ORDER BY.
Example:
SELECT * FROM students
ORDER BY age DESC;
This will list students in descending order of age (oldest first).
8. LIMIT: Fetching a Limited Number of Rows
If you want to restrict the number of rows returned, use LIMIT.
Example:
SELECT * FROM students
LIMIT 5;
This will show only the first 5 students.
Quick Tips for Using SELECT:
- Always specify the columns you actually need for better performance
- Use WHERE, ORDER BY, and LIMIT together to control what data is shown
- Combine DISTINCT and ORDER BY to retrieve unique, sorted lists
- Use meaningful aliases for better report generation
Conclusion
The SELECT statement is the foundation of retrieving and working with data in SQL. By mastering SELECT, you gain the ability to analyze, filter, and sort information — essential skills for database management and real-world application development.