SQL Syntax

SQL statements follow a simple, consistent structure built from keywords, the tables you act on, and the conditions that shape the result.

The shape of an SQL statement

Every SQL command is called a statement. A statement begins with a keyword that describes the action you want, such as SELECT, INSERT, UPDATE, or DELETE. After the keyword you list the details the database needs, for example which columns to read and which table to read from. Learning the basic pattern once makes every later topic easier to follow.

The statement below is a complete query. It reads two columns from the Customers table but only for rows where the country is Germany. Notice how it reads almost like an English sentence: select these columns, from this table, where this condition is true.

Example

SELECT CustomerName, City
FROM Customers
WHERE Country = 'Germany';

Common SQL keywords

KeywordWhat it does
SELECTReads and returns data from one or more tables
FROMNames the table the data should come from
WHEREFilters rows so only matching records are returned
INSERT INTOAdds new rows to a table
UPDATEChanges values in existing rows
DELETERemoves rows from a table

The semicolon

The semicolon marks the end of a statement. It becomes important when you want to run several statements one after another, because it tells the database where one command stops and the next begins. Many systems accept a single statement without a semicolon, but it is good practice to include it every time.

Example

SELECT * FROM Products;
SELECT * FROM Orders;

Case sensitivity and whitespace

  • SQL keywords are not case sensitive: SELECT, select, and Select all work the same way
  • Writing keywords in uppercase is a common convention that makes statements easier to read
  • Extra spaces, tabs, and line breaks between words are ignored, so you can format a query across several lines
  • Text values inside quotes, such as 'Germany', are case sensitive on many systems, so match them carefully
Note: Splitting a long statement across multiple lines, with each clause on its own line, does not change how it runs but makes it far easier to read and to fix later.

With this pattern in mind you are ready to explore the most important statement in SQL. Continue to SQL Select to start reading real data.

Exercise: SQL Syntax

Which symbol is conventionally used to terminate a SQL statement?