TExcelTAD
Home › MySQL › MySQL SQL Syntax

MySQL SQL Syntax

SQL is a declarative language: you describe what result you want, and the database works out how to get it. A request to the database is called a statement. A statement that reads data is usually called a query.

mysql · try it yourself
SELECT first_name, city
FROM customers
WHERE country = 'Germany';
+------------+--------+
| first_name | city   |
+------------+--------+
| Daniel     | Berlin |
| Noah       | Munich |
+------------+--------+

That statement has three clauses, each starting with a keyword: SELECT names the columns, FROM names the table and WHERE filters the rows.

Rules of the road

Rule Details
Statements end with ; Required when you send several statements at once (as every playground here does)
Keywords are case-insensitive select, SELECT and Select are the same. Writing keywords in UPPER CASE is a convention that makes queries easier to read
Whitespace is free Line breaks and indentation are ignored; use them to lay out long queries
Strings use single quotes 'Berlin'. MySQL also accepts double quotes, but standard SQL (and MySQL's ANSI_QUOTES mode) treats "..." as a name
Names may be quoted with backticks `order` lets you use a reserved word or a space in a table/column name

Table and column names are case-insensitive for columns, but table names follow the operating system by default: on Linux Customers and customers are different tables, on Windows and macOS they are the same. Stick to lower case and you never have to think about it.

mysql · try it yourself
select FIRST_NAME, Last_Name
FROM   customers
where  customer_id = 2;
+------------+-----------+
| FIRST_NAME | Last_Name |
+------------+-----------+
| Ben        | Carter    |
+------------+-----------+

The most important statements

Statement Purpose
SELECT read data
INSERT INTO add new rows
UPDATE change existing rows
DELETE remove rows
CREATE DATABASE / CREATE TABLE create a database / a table
ALTER TABLE change a table's structure
DROP TABLE / DROP DATABASE delete a table / a whole database
CREATE INDEX speed up searches on a column

The first four manipulate data and are known as DML (Data Manipulation Language). The CREATE/ALTER/DROP family defines structure and is known as DDL (Data Definition Language). GRANT and REVOKE, which control who may do what, are DCL.

Several statements in one script

When the client reads a script, it executes the statements in order. Each SELECT prints its own table:

mysql · try it yourself
SELECT name FROM categories WHERE category_id = 1;
SELECT name FROM categories WHERE category_id = 2;
+-----------+
| name      |
+-----------+
| Beverages |
+-----------+
+--------+
| name   |
+--------+
| Bakery |
+--------+

Reserved words and backticks

ORDER, GROUP, KEY and DESC are reserved words. Using one as a name fails with a syntax error unless you wrap it in backticks:

mysql · try it yourself
SELECT 'ok' AS `status`, 3 AS `desc`;
+--------+------+
| status | desc |
+--------+------+
| ok     |    3 |
+--------+------+

Without the backticks, the same statement stops the script with error 1064, MySQL's generic syntax error:

sql
SELECT 3 AS desc;
-- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
-- corresponds to your MySQL server version for the right syntax to use near 'desc' at line 1

The message quotes the text near the problem, which is the best clue for finding it. (MariaDB's message is the same except that it says "your MariaDB server version".)

Test yourself

Which of these is true about SQL keywords in MySQL?

  1. They must be upper case
  2. They must be lower case
  3. They are case-insensitive
  4. Only SELECT is case-insensitive

How do you use the reserved word order as a column name?

  1. 'order'
  2. [order]
  3. order (backticks)
  4. You can't

CREATE TABLE and ALTER TABLE belong to which group of statements?

  1. DML
  2. DDL
  3. DCL
  4. TCL