YouTip LogoYouTip

Sql Syntax

```html\\n\\n SQL Syntax |Runoob Tutorial\\n\\n

SQL Syntax |Runoob Tutorial

\\n

SQL (Structured Query Language) is a standard language for managing and manipulating relational databases, including data querying, data insertion, data updating, data deletion, database structure creation and modification, etc.

\\n\\n Image 1\\n\\n
\\n\\n

Database Table

\\n

A database usually contains one or more tables, each table has a name identifier (e.g., "Websites"), and the table contains records (rows) with data.

\\n

In this tutorial, we created a Websites table in the database of MySQL to store website records.

\\n

We can view the data in the "Websites" table with the following command:

\\n
\\nmysql> use ;\\nDatabase changed\\nmysql> set names utf8;\\nQuery OK, 0 rows affected (0.00 sec)\\nmysql> SELECT * FROM Websites;\\n+----+--------------+---------------------------+-------+---------+\\n| id | name         | url                       | alexa | country |\\n+----+--------------+---------------------------+-------+---------+\\n|  1 | Google       | https://www.google.cm/    |     1 | USA     |\\n|  2 | Taobao         | https://www.taobao.com/   |    13 | CN      |\\n|  3 |      |     |  4689 | CN      |\\n|  4 | Weibo         | http://weibo.com/         |    20 | CN      |\\n|  5 | Facebook     | https://www.facebook.com/ |     3 | USA     |\\n+----+--------------+---------------------------+-------+---------+\\n5 rows in set (0.01 sec)\\n    
\\n

Explanation

\\n
    \\n
  • use ; command is used to select the database.
  • \\n
  • set names utf8; command is used to set the character set to be used.
  • \\n
  • SELECT * FROM Websites; reads the information from the data table.
  • \\n
  • The above table contains five records (each corresponding to website information) and five columns (id, name, url, alexa, and country).
  • \\n
\\n\\n
\\n\\n

SQL Statements

\\n

Most of the work you need to do on the database is completed by SQL statements.

\\n

The following SQL statement selects all records from the "Websites" table:

\\n

Example

\\n
SELECT * FROM Websites;
\\n

In this tutorial, we will explain various different SQL statements for you.

\\n\\n
\\n\\n

Remember...

\\n
    \\n
  • SQL is not case-sensitive: SELECT is the same as select.
  • \\n
\\n\\n
\\n\\n

Semicolon after SQL Statements?

\\n

Some database systems require a semicolon at the end of each SQL statement.

\\n

The semicolon is the standard way to separate SQL statements in database systems, so you can execute more than one SQL statement in the same request to the server.

\\n

In this tutorial, we will use a semicolon at the end of each SQL statement.

\\n\\n
\\n\\n

Some of the Most Important SQL Commands

\\n
    \\n
  • SELECT - extracts data from a database
  • \\n
  • UPDATE - updates data in a database
  • \\n
  • DELETE - deletes data from a database
  • \\n
  • INSERT INTO - inserts new data into a database
  • \\n
  • CREATE DATABASE - creates a new database
  • \\n
  • ALTER DATABASE - modifies a database
  • \\n
  • CREATE TABLE - creates a new table
  • \\n
  • ALTER TABLE - modifies a database table
  • \\n
  • DROP TABLE - deletes a table
  • \\n
  • CREATE INDEX - creates an index (search key)
  • \\n
  • DROP INDEX - deletes an index
  • \\n
\\n

Here are some commonly used SQL statements and syntax:

\\n

SELECT: used to query data from a database.

\\n
SELECT column_name(s) FROM table_name WHERE condition ORDER BY column_name [ASC|DESC]
\\n
    \\n
  • column_name(s): columns to query.
  • \\n
  • table_name: table to query.
  • \\n
  • condition: query condition (optional).
  • \\n
  • ORDER BY: sorting method, ASC means ascending, DESC means descending (optional).
  • \\n
\\n

INSERT INTO: used to insert new data into a database table.

\\n
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...)
\\n
    \\n
  • table_name: table to insert data into.
  • \\n
  • column1, column2, ...: columns to insert data into.
  • \\n
  • value1, value2, ...: values corresponding to the columns.
  • \\n
\\n

UPDATE: used to update existing data in a database table.

\\n
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition
\\n
    \\n
  • table_name: table to update data in.
  • \\n
  • column1 = value1, column2 = value2, ...: columns to update and their new values.
  • \\n
  • condition: update condition.
  • \\n
\\n

DELETE: used to delete data from a database table.

\\n
DELETE FROM table_name WHERE condition
\\n
    \\n
  • table_name: table to delete data from.
  • \\n
  • condition: delete condition.
  • \\n
\\n

CREATE TABLE: used to create a new database table.

\\n
CREATE TABLE table_name ( column1 data_type constraint, column2 data_type constraint, ...)
\\n
    \\n
  • table_name: name of the table to create.
  • \\n
  • column1, column2, ...: columns of the table.
  • \\n
  • data_type: data type of the column (such as INT, VARCHAR, etc.).
  • \\n
  • constraint: constraints of the column (such as PRIMARY KEY, NOT NULL, etc.).
  • \\n
\\n

ALTER TABLE: used to modify the structure of an existing database table.

\\n
ALTER TABLE table_name ADD column_name data_type
\\n
    \\n
  • table_name: table to modify.
  • \\n
  • column_name: column to add.
  • \\n
  • data_type: data type of the column.
  • \\n
\\n

or:

\\n
ALTER TABLE table_name DROP COLUMN column_name
\\n
    \\n
  • column_name: column to delete.
  • \\n
\\n

DROP TABLE: used to delete a database table.

\\n
DROP TABLE table_name
\\n
    \\n
  • table_name: table to delete.
  • \\n
\\n

CREATE INDEX: used to create an index to speed up query speed.

\\n
CREATE INDEX index_name ON table_name (column_name)
\\n
    \\n
  • index_name: name of the index.
  • \\n
  • column_name: column to index.
  • \\n
\\n

DROP INDEX: used to delete an index.

\\n
DROP INDEX index_name ON table_name
\\n
    \\n
  • index_name: name of the index to delete.
  • \\n
  • table_name: table where the index is located.
  • \\n
\\n

WHERE: used to specify filter conditions.

\\n
SELECT column_name(s) FROM table_name WHERE condition
\\n
    \\n
  • condition: filter condition.
  • \\n
\\n

ORDER BY: used to sort the result set.

\\n
SELECT column_name(s) FROM table_name ORDER BY column_name [ASC|DESC]
\\n
    \\n
  • column_name: column used for sorting.
  • \\n
  • ASC: ascending (default).
  • \\n
  • DESC: descending.
  • \\n
\\n

GROUP BY: used to group the result set by one or more columns.

\\n
SELECT column_name(s), aggregate_function(column_name) FROM table_name WHERE condition GROUP BY column_name(s)
\\n
    \\n
  • aggregate_function: aggregate function (such as COUNT, SUM, AVG, etc.).
  • \\n
\\n

HAVING: used to filter the result set after grouping.

\\n
SELECT column_name(s), aggregate_function(column_name) FROM table_name GROUP BY column_name(s) HAVING condition
\\n
    \\n
  • condition: filter condition.
  • \\n
\\n

JOIN: used to combine records from two or more tables.

\\n
SELECT column_name(s) FROM table_name1 JOIN table_name2 ON table_name1.column_name = table_name2.column_name
\\n
    \\n
  • JOIN: can be INNER JOIN, LEFT JOIN, RIGHT JOIN, or FULL JOIN.
  • \\n
\\n

DISTINCT: used to return unique different values.

\\n
SELECT DISTINCT column_name(s) FROM table_name
\\n
    \\n
  • column_name(s): columns to query.
  • \\n
\\n\\n```
← Sql SelectSql Intro β†’