Open In App

MySQL SELECT Statement

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL SELECT Statement is used to print data from one or more tables. The data returned is stored in the result table, also called as result set.

It is one of the most commonly used statements in MySQL. In this article, we will learn about MySQL SELECT statement, from its basic syntax to advanced usage with examples.

SELECT Statement in MySQL

The SELECT Statement in MySQL retrieves data from one or more tables in a database. Its primary function is to query the database and return a result set. It can be used to retrieve specific data from a specific table.

It can also be used to print text and perform simple mathematical operations in MySQL. For example SELECT 14+15 and SELECT “HELLO WORLD”.

The MySQL SELECT statement is used with various clauses like WHERE, GROUP BY, ORDER BY, etc. We can also use aggregate functions like SUM, COUNT, etc with SELECT statement to summarize data.

Syntax

The Basic Syntax of the MySQL SELECT Statement is as follows:

SELECT column1, column2, …
FROM table_name

Here, column1, column2, … are the columns you want to retrieve. If you want to retrieve data from all columns/fields, you can use the following syntax:

SELECT * FROM table_name

Demo MySQL Database

For this tutorial on MySQL SELECT statement, we will use the following MySQL table.

employee_idfirst_namelast_namesalary
1JohnDoe50000
2JaneSmith60000
3RobertJohnson75000

To quickly create this table on your local MySQL Workbench, enter the following MySQL query:

MySQL
CREATE TABLE employees (
     employee_id INT PRIMARY KEY,
     first_name VARCHAR(50),
     last_name VARCHAR(50),
     salary DECIMAL(10, 2) );
INSERT INTO employees VALUES
     (1, 'John', 'Doe', 50000),
     (2, 'Jane', 'Smith', 60000),
     (3, 'Robert', 'Johnson', 75000);

MySQL SELECT Statement Examples

Let’s explore some examples to learn how to write SELECT statement queries.

Example 1: SELECT Columns

Retrieve only first_name and last_name columns

SELECT first_name, last_name
FROM employees;

This example retrieves the first_name and last_name columns from the employees table.

Output:

select column example output

Simple Select Query

Explanation:

  • The query retrieves the first_name and last_name columns from the employees table.
  • The result set displays the names of all employees in the table.

Example 2: SELECT * (Select Entire Table)

This query will retrieve the entire employee table.

SELECT * from employees;

Output:

employee_idfirst_namelast_namesalary
1JohnDoe50000
2JaneSmith60000
3RobertJohnson75000

Example 3: SELECT without table

SELECT 32*32

Output:

32*32
1024

We can also use SELECT statement to perform, basic mathematical operations.

MySQL SELECT DISTINCT Statement

MySQL SELECT DISTINCT Statement is used to retrieve only distinct data from a field/column.

It is very used to remove duplicates from the results.

Syntax

SELECT DISTINCT column1, column2, …
FROM table_name

Key Takeaways About MySQL SELECT Statement

  • MySQL SELECT statement is fundamental for anyone working with relational databases.
  • It is used to retrieve data from one or multiple tables.
  • It can be used to retrieve specific columns or the entire table using SELECT statement.
  • We can also use SELECT statement to print mathematical operations.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads