MySQL SELECT Statement

MySQL SELECT Statement

The MySQL SELECT statement is used to fetch data from the one or more tables in MySQL. We can retrieve records of all fields or specified fields.

Syntax for specified fields:

  1. SELECT expressions
  2. FROM tables
  3. [WHERE conditions];

Syntax for all fields:

  1. SELECT * FROM tables [WHERE conditions];

MySQL SELECT Example 1: for specified fields

In such case, it is mandatory to specify field names.

Example:

  1. SELECT officer_name, address
  2. FROM officers

q1

MySQL SELECT Example 2: for all fields

In such case, we can specify either all fields or * (asterisk) symbol.

  1. SELECT * FROM officers

q2

MySQL SELECT Example 3: from multiple tables

MySQL SELECT statement can also be used to retrieve records from multiple tables by using JOIN statement.

Let’s take two tables “students” and “officers”, having the following data.

q3

Execute the following query:

  1. SELECT officers.officer_id, students.student_name
  2. FROM students
  3. INNER JOIN officers
  4. ON students.student_id = officers.officer_id
  5. ORDER BY student_id;

Output:

101