fbpx

MySQL SELECT Statement

This SQL tutorial demonstrates how to create a basic SELECT statement in MySQL, and provides explanations, examples and exercises. For this lesson’s exercises, use this link.

This tutorial is a part of several posts explaining how to write basic queries in MySQL. To read additional posts regarding this subject, please use the following links:


Basic Select Statement

the syntax for a basic SELECT statement in MySQL is:

SELECT *
FROM table_name

For example :

SELECT *
FROM books

In this example we displayed all columns of data in Books table.

The MySQL SELECT clause:

  • Let you choose what you want to display.
  • The asterisk sign (*) indicates that you want to select all fields contained in this table.

The MySQL FROM clause:

  • Let you specify from which table you want to retrieve all of these fields.
  • A table’s name always appears after the FROM keyword.

Selecting Specific Columns

the syntax for selecting specific columns in MySQL is:

SELECT column_name, column_name, column_name ..
FROM table_name

For example :

SELECT bookID , bookName
FROM books

The example above retrieves specific columns.

The MySQL SELECT clause:

  • Let you choose what columns you want to display.
  • After the MySQL SELECT keyword, specify the names of the columns that you would like to retrieve, separated by comma (,).
  • You can specify as many columns as you want; you can even specify the same column more than once.
  • The columns appear in the order selected.

The MySQL FROM clause:

  • Let you specify from which table you want to display these columns.
  • A table’s name always appears after the MySQL FROM keyword.

General Guidelines

  • In each SQL statement, MySQL SELECT and FROM clauses are mandatory. Without both of them, SQL statements are not valid (there is no point in seeking to display data without indicating what should be retrieved and from where).
-- SQL Statement 1 - not valid (FROM clause is missing)
SELECT bookName, bookPrice, Publisher

-- SQL Statement 2 - not valid (SELECT clause is missing)
FROM books 

-- SQL Statement 3 - valid
SELECT bookName, bookPrice, Publisher
FROM books
  • The order of the MySQL SELECT and FROM clauses cannot be changed, the MySQL SELECT clause will always be listed first; you cannot write an MySQL SELECT statement that begins with a FROM clause.
-- Not a valid SQL statement
FROM books
SELECT bookName, bookPrice, Publisher
  • It is possible to specify a column’s name multiple times; the data of this column will simply appear again and again, according to the number of times the column was specified.
SELECT bookID, bookName, bookPrice, bookPrice
FROM books
  • To enhance readability – even though the MySQL SQL syntax is neither case-sensitive, nor sensitive to spaces or line breaks, ensure writing in an orderly manner: write the keywords in capital letters, names of columns/tables in small letters, insert a line break after each command and indents when required.

UpScale Analytics is one of the largest platforms in the world for learning SQL by doing, consisting over 300 SQL exercises at different levels (including solutions), by topics, across over 100 different datasets. More…