SQL Syntax
SQL (Structured Query Language) is a standard language for managing and manipulating relational databases, including functions such as data query, data insertion, data update, data deletion, and database structure creation and modification.

Database Tables
A database usually contains one or more tables. Each table is identified by a name (for example: "Websites"), and a table contains records (rows) with data.
In this tutorial, we created the Websites table in the RUNOOB database in MySQL to store website records.
We can view the data in the "Websites" table with the following command:
mysql> use RUNOOB;
Database changed
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | Runoob Tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
5 rows in set (0.01 sec)
Parsing
- use RUNOOB; The command is used to select a database.
- set names utf8; The command is used to set the character set to use.
- SELECT * FROM Websites; Read information about a data table.
- The table above contains five records (each corresponding to one website) and 5 columns (id, name, url, alexa, and country).
SQL statements
Most of the work you need to do on a database is accomplished with SQL statements.
The following SQL statement selects all records from the "Websites" table:
Example
SELECT
FROM
Websites
;
In this tutorial, we will explain various SQL statements to you.
Remember...
- SQL is case-insensitive: SELECT is the same as select.
Semicolon after SQL statements?
Some database systems require a semicolon at the end of each SQL statement.
A semicolon is the standard way to separate each SQL statement in a database system, allowing more than one SQL statement to be executed in the same request to the server.
In this tutorial, we will use a semicolon at the end of each SQL statement.
Some of the Most Important SQL Commands
- SELECT - Extract data from a database
- UPDATE - Update data in a database
- DELETE - Delete data from a database
- INSERT INTO - Insert new data into a database
- CREATE DATABASE - Create a new database
- ALTER DATABASE - Modify a database
- CREATE TABLE - Create a new table
- ALTER TABLE - Alter (change) a database table
- DROP TABLE - Delete a table
- CREATE INDEX - Create an index (search key)
- DROP INDEX - Delete an index
The following are some commonly used SQL statements and syntax:
SELECT: Used to query data from a database.
SELECT column_name(s)
FROM table_name
WHERE condition
ORDER BY column_name [ASC|DESC]
column_name(s): The columns to query.table_name: The table to query.condition: Query conditions (optional).ORDER BY: Sort order,ASCIndicates ascending order,DESCindicates descending order (optional).
INSERT INTO: Used to insert new data into a database table.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
table_name: The table into which data is inserted.column1, column2, ...: The columns into which data is inserted.value1, value2, ...: The values for the corresponding columns.
UPDATE: Used to update existing data in a database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition
table_name: The table whose data is updated.column1 = value1, column2 = value2, ...: The columns to update and their new values.condition: The update condition.
DELETE: Used to delete data from a database table.
DELETE FROM table_name
WHERE condition
table_name: The table from which data is deleted.condition: The deletion condition.
CREATE TABLE: Used to create a new database table.
CREATE TABLE table_name (
column1 data_type constraint,
column2 data_type constraint,
...
)
table_name: The name of the table to create.column1, column2, ...: The columns of the table.data_type: The data type of a column (such asINT、VARCHARetc.).constraint: The constraint on a column (such asPRIMARY KEY、NOT NULLetc.).
ALTER TABLE: Used to modify the structure of an existing database table.
ALTER TABLE table_name
ADD column_name data_type
table_name: The table to modify.column_name: The column to add.data_type: The data type of the column.
Or:
ALTER TABLE table_name
DROP COLUMN column_name
column_name: The column to delete.
DROP TABLE: Used to delete a database table.
DROP TABLE table_name
table_name: The table to delete.
CREATE INDEX: Used to create an index to speed up queries.
CREATE INDEX index_name
ON table_name (column_name)
index_name: The name of the index.column_name: The column to index.
DROP INDEX: Used to delete an index.
DROP INDEX index_name
ON table_name
index_name: The name of the index to delete.table_name: The table where the index is located.
WHERE: Used to specify filter conditions.
SELECT column_name(s)
FROM table_name
WHERE condition
condition: The filter condition.
ORDER BY: Used to sort a result set.
SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
column_name: The column to sort by.ASC: Ascending order (default).DESC: Descending order.
GROUP BY: Used to group a result set by one or more columns.
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s)
aggregate_function: Aggregate functions (such as COUNT, SUM, AVG, etc.).
HAVING: Used to filter a grouped result set.
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
GROUP BY column_name(s)
HAVING condition
condition: The filter condition.
JOIN: Used to combine records from two or more tables.
SELECT column_name(s)
FROM table_name1
JOIN table_name2
ON table_name1.column_name = table_name2.column_name
JOIN: Can be INNER JOIN, LEFT JOIN, RIGHT JOIN, or FULL JOIN.
DISTINCT: Used to return only distinct values.
SELECT DISTINCT column_name(s)
FROM table_name
column_name(s): The columns to query.