SQL Syntax

SQL Statements

Most actions on a database are done with SQL statements. A statement is made of keywords (like SELECT and FROM), names (tables and columns) and values.

Example

The following statement selects all records in the Customers table:

SELECT * FROM Customers;

Result: 8 rows

CustomerIDCustomerNameContactNameCityCountry
1Aarav TradersAarav ShahMumbaiIndia
2Blue Harbor FoodsEmma ClarkLondonUK
3Cedar & CoLiam BrownTorontoCanada

… 5 more rows

Keep in Mind

  • SQL keywords are NOT case sensitive: select is the same as SELECT. By convention keywords are written in UPPER CASE.
  • Table and column names may be case sensitive depending on the database and operating system (e.g. MySQL on Linux).
  • Text values go inside single quotes: 'India'. Numbers are written without quotes.
  • A statement can span several lines; spaces and line breaks are ignored.

Semicolon after SQL Statements?

The semicolon is the standard way to separate statements, which lets you send more than one statement to the server in one call. Some tools do not require it for a single statement, but it is good practice to always end statements with ;.

Comments

Comments explain your SQL and are ignored when the statement runs.

Example

Single-line comments start with --; multi-line comments go between /* and */:

-- Customers from India
SELECT CustomerName, City
FROM Customers
/* Only rows where the country
   is India are returned */
WHERE Country = 'India';

Result: 2 rows

CustomerNameCity
Aarav TradersMumbai
Delta MartBengaluru

Some of The Most Important SQL Commands

CommandWhat it does
SELECTextracts data from a database
UPDATEupdates data in a database
DELETEdeletes data from a database
INSERT INTOinserts new data into a database
CREATE DATABASEcreates a new database
CREATE TABLEcreates a new table
ALTER TABLEmodifies a table
DROP TABLEdeletes a table
CREATE INDEXcreates an index (search key)
DROP INDEXdeletes an index