Use SQL SELECT with examples
The SELECT statement is an essential part of working with databases in SQL, and is used to retrieve data from a database table. To learn the SELECT statement, it is helpful to have a sample database table to work with. Here is an example table that we can use of below example Table
Here’s an example table called “students” with sample data:
id | name | age | grade |
1 | Alice | 18 | 90 |
2 | Bob | 19 | 80 |
3 | Eve | 18 | 95 |
4 | Mallory | 20 | 75 |
5 | Trent | 18 | 85 |
Select all columns and rows from a table:
Syntax:
SELECT * FROM table_name;
Example:
SELECT * FROM students;
This statement retrieves all columns and rows from the students table. The * is a wildcard that stands for “all.”
Output:
id | name | age | grade |
1 | Alice | 18 | 90 |
2 | Bob | 19 | 80 |
3 | Eve | 18 | 95 |
4 | Mallory | 20 | 75 |
5 | Trent | 18 | 85 |
Select specific columns from a table:
Syntax:
SELECT column_name1, column_name2 FROM table_name;
Example:
SELECT name, grade FROM students;
This statement retrieves the name and grade columns from the students table.
Output:
name | grade |
Alice | 90 |
Bob | 80 |
Eve | 95 |
Mallory | 75 |
Trent | 85 |
Select distinct values from a table:
Syntax:
SELECT DISTINCT column_name FROM table_name;
Example:
SELECT DISTINCT age FROM students;
This statement retrieves the distinct values in the age column from the students table. If there are multiple rows with the same value in the age column, only one instance of that value will be returned.
Output:
age |
18 |
19 |
20 |
Select rows that meet certain criteria:
Syntax:
SELECT * FROM table_name WHERE column_name = ‘some value’;
Example:
SELECT * FROM students WHERE age = 18;
This statement retrieves all columns and rows from the students table where the age column is equal to 18.
Output:
id | name | age | grade |
1 | Alice | 18 | 90 |
3 | Eve | 18 | 95 |
5 | Trent | 18 | 85 |
Select rows with multiple criteria (using AND operator):
Syntax:
SELECT * FROM table_name WHERE column_name1 = ‘some value’ AND column_name2 = ‘some value’;
This query will retrieve all rows from the students table where the age column is equal to 18 and the grade column is greater than 85.
Example:
SELECT * FROM students WHERE age = 18 AND grade > 85;
Output:
id | name | age | grade |
1 | Alice | 18 | 90 |
3 | Eve | 18 | 95 |
Select rows with multiple criteria (using OR operator):
Syntax:
SELECT * FROM table_name WHERE column_name1 = ‘some value’ OR column_name2 = ‘some value’;
Example:
SELECT * FROM students WHERE age = 18 OR grade > 85;
This statement retrieves all columns and rows from the students table where the age column is equal to 18 or the grade column is greater than 85.
Output:
id | name | age | grade |
1 | Alice | 18 | 90 |
3 | Eve | 18 | 95 |
5 | Trent | 18 | 85 |
Select rows and sort the results:
Syntax:
SELECT * FROM table_name ORDER BY column_name ASC/DESC;
Example:
SELECT * FROM students ORDER BY grade ASC;
This statement retrieves all columns and rows from the students table and sorts the results in ascending order by the grade column.
Output:
id | name | age | grade |
4 | Mallory | 20 | 75 |
2 | Bob | 19 | 80 |
5 | Trent | 18 | 85 |
1 | Alice | 18 | 90 |
3 | Eve | 18 | 95 |
Select rows and sort the results by multiple columns:
Syntax:
SELECT * FROM table_name ORDER BY column_name1 ASC/DESC, column_name2 ASC/DESC;
Example:
SELECT * FROM students ORDER BY age ASC, grade DESC;
This statement retrieves all columns and rows from the students table and sorts the results first by the age column in ascending order and then by the grade column in descending order.
Output:
id | name | age | grade |
3 | Eve | 18 | 95 |
1 | Alice | 18 | 90 |
5 | Trent | 18 | 85 |
2 | Bob | 19 | 80 |
4 | Mallory | 20 | 75 |