SQL SELECT Statement

The SQL SELECT statement is used to retrieve data from a database. The data returned by a SELECT query is called a result set, which is structured in rows and columns according to the specified criteria in the query.

In this tutorial, we will go through the syntax of SQL SELECT statement, detailed explanation for each part of the syntax, and then an example using MySQL 8.0 with queries and results from the Workbench.


Basic Syntax of the SQL SELECT Statement

The basic syntax of a SQL SELECT statement is as follows:

</>
Copy
SELECT select_list [ INTO new_table ]
[ FROM table_source ] [ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]

Each part of this syntax has a specific purpose:

  • SELECT: Specifies the columns to be retrieved. It can be specific columns or * to select all columns.
  • INTO: Optional. Creates a new table to store the result.
  • FROM: Specifies the table(s) from which to retrieve data.
  • WHERE: Filters rows based on a specified condition.
  • GROUP BY: Groups rows based on one or more columns.
  • HAVING: Filters groups after grouping, typically used with aggregate functions.
  • ORDER BY: Sorts the result set by one or more columns, in ascending (default) or descending order.

Step-by-Step Guide with MySQL

We’ll walk through each part of the SELECT syntax using a database and table in MySQL 8.0 with MySQL Workbench. The examples will focus on a sample students table with fields id, name, age, grade, and locality.

Step 1: Setting up the Database

1. Open MySQL Workbench.

2. To create a new database, run:

</>
Copy
CREATE DATABASE school;
SQL SELECT Example -

3. Select the school database to use in subsequent queries:

</>
Copy
USE school;
SQL SELECT Example -

Step 2: Creating the Table

Create a students table with fields for demonstration:

</>
Copy
CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    age INT,
    grade VARCHAR(10),
    locality VARCHAR(50)
);
SQL SELECT Example -

Step 3: Inserting Sample Data into the Table

Insert some records into the students table for use in the examples:

</>
Copy
INSERT INTO students (name, age, grade, locality)
VALUES
('Alice', 14, '8th', 'Northside'),
('Bob', 15, '9th', 'Westend'),
('Charlie', 14, '8th', 'Northside'),
('David', 16, '10th', 'Southend'),
('Eva', 15, '9th', 'Westend');
SQL SELECT Example -

After inserting, the table students will look like this:

SQL SELECT Example -

Examples of SQL SELECT Statement

Below are examples of different forms of the SELECT statement, explaining each part of the syntax.


1. Selecting All Columns

This query selects all columns from the students table:

</>
Copy
SELECT * 
FROM students;
SQL SELECT Example - Selecting All Columns

2. Selecting Specific Columns

This query selects only the name and age columns:

</>
Copy
SELECT name, age 
FROM students;
SQL SELECT Example - Selecting Specific Columns

3. Using WHERE Clause to Filter Rows

This query selects students who are 15 years old:

</>
Copy
SELECT * 
FROM students 
WHERE age = 15;
SQL SELECT Example - Using WHERE Clause to Filter Rows

4. Using GROUP BY Clause

This query groups students by their grade:

</>
Copy
SELECT grade, COUNT(*) AS student_count 
FROM students 
GROUP BY grade;
SQL SELECT Example - Using GROUP BY Clause

5. Using HAVING with GROUP BY

This query shows only grades with more than one student:

</>
Copy
SELECT grade, COUNT(*) AS student_count
FROM students
GROUP BY grade
HAVING COUNT(*) > 1;
SQL SELECT Example - Using HAVING with GROUP BY

6. Sorting Results with ORDER BY

This query orders students by their age in descending order:

</>
Copy
SELECT * 
FROM students 
ORDER BY age DESC;
SQL SELECT Example - Sorting Results with ORDER BY

7. Combining WHERE and ORDER BY

This query selects students from Westend and orders them by name:

</>
Copy
SELECT * 
FROM students 
WHERE locality = 'Westend' 
ORDER BY name;
SQL SELECT Example - Combining WHERE and ORDER BY

8. Using WHERE with LIKE Operator

This query finds students whose name contains li:

</>
Copy
SELECT * 
FROM students 
WHERE name LIKE '%li%';
SQL SELECT Example - Using WHERE with LIKE Operator

Frequently Asked Questions (FAQ)

1. What does the SQL SELECT statement do?

The SQL SELECT statement is used to retrieve data from a database. It can return specific columns or all columns from a table, based on criteria defined by the user.

2. How do I select all columns from a table?

To select all columns from a table, use the * symbol with SELECT, like this:

</>
Copy
SELECT * FROM table_name;

3. How do I filter results in a SELECT query?

To filter results, use the WHERE clause in the SELECT query. For example, to select rows where age is greater than 18:

</>
Copy
SELECT * FROM students WHERE age > 18;

4. How can I sort the results of a SELECT query?

You can sort results using the ORDER BY clause. By default, it sorts in ascending order, but you can use DESC to sort in descending order:

</>
Copy
SELECT * FROM students ORDER BY age DESC;

5. What is the difference between WHERE and HAVING in SQL?

The WHERE clause filters rows before grouping, while the HAVING clause filters groups after GROUP BY. For example:

</>
Copy
SELECT grade, COUNT(*) FROM students GROUP BY grade HAVING COUNT(*) > 2;