Blog

Blog

Learn how to use SQL SELECT with examples

Use SQL SELECT with examples

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:

idnameagegrade
1Alice1890
2Bob1980
3Eve1895
4Mallory2075
5Trent1885

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:

idnameagegrade
1Alice1890
2Bob1980
3Eve1895
4Mallory2075
5Trent1885

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:

namegrade
Alice90
Bob80
Eve95
Mallory75
Trent85

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:

idnameagegrade
1Alice1890
3Eve1895
5Trent1885

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:

idnameagegrade
1Alice1890
3Eve1895

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:

idnameagegrade
1Alice1890
3Eve1895
5Trent1885

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:

idnameagegrade
4Mallory2075
2Bob1980
5Trent1885
1Alice1890
3Eve1895

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:

idnameagegrade
3Eve1895
1Alice1890
5Trent1885
2Bob1980
4Mallory2075
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!