Lesson 2 +10 XP

SQL Syntax

SQL Syntax

Almost everything you do in SQL is a statement. A statement is a command that tells the database what to do.

The most common statement

SELECT * FROM Customers;
  • SELECT - "give me data".
  • * - "all columns".
  • FROM Customers - "from the Customers table".
  • ; - every statement ends with a semicolon.

Some more examples

SELECT CustomerName, City FROM Customers;

SELECT CustomerName FROM Customers WHERE Country = 'Germany';

INSERT INTO Customers (CustomerName, City) VALUES ('Cardinal', 'Stavanger');

The three main parts

  1. SELECT - which columns you want.
  2. FROM - which table they come from.
  3. WHERE (optional) - which rows to filter by.

Notes on text and numbers

  • Text values go in single quotes: 'Germany'.
  • Numbers have no quotes: WHERE Price > 30.

Comments

Use -- for a single-line comment and / ... / for a multi-line comment:

-- Select all customers
SELECT * FROM Customers;

TL;DR

  • SQL statements tell the database what to do and end with ;.
  • SELECT columns FROM table WHERE condition is the core pattern.
  • Text uses single quotes; numbers do not.
  • -- starts a comment.