codegurukool.com · Free coding tutorials & online compiler

Home › SQL Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

SELECT

Definition: Reads data from a table. List the columns you want, or use * for all columns.

Syntax:

SELECT columns FROM table;

Example:

SELECT name, city FROM Customers;

FROM

Definition: Names the table that the query reads from.

Syntax:

SELECT ... FROM table;

Example:

SELECT * FROM Customers;

WHERE

Definition: Keeps only the rows that satisfy a condition. Combine conditions with AND and OR.

Syntax:

... WHERE condition;

Example:

SELECT * FROM Customers WHERE country = 'UK';

ORDER BY

Definition: Sorts the result by one or more columns. ASC is ascending (default), DESC is descending.

Syntax:

... ORDER BY column ASC|DESC;

Example:

SELECT * FROM Orders ORDER BY amount DESC;

INSERT INTO

Definition: Adds a new row to a table. List the columns and then the matching values.

Syntax:

INSERT INTO table (cols) VALUES (values);

Example:

INSERT INTO Customers (name, country) VALUES ('Fay', 'UK');

UPDATE

Definition: Changes values in existing rows. Always add WHERE, or every row will change.

Syntax:

UPDATE table SET column = value WHERE condition;

Example:

UPDATE Customers SET city = 'Delhi' WHERE id = 4;

DELETE FROM

Definition: Removes rows from a table. Without WHERE it deletes all rows.

Syntax:

DELETE FROM table WHERE condition;

Example:

DELETE FROM Orders WHERE id = 3;

CREATE TABLE

Definition: Creates a new table with named columns and their data types.

Syntax:

CREATE TABLE name (col type, ...);

Example:

CREATE TABLE Pets (id INTEGER PRIMARY KEY, name TEXT);

JOIN

Definition: Combines rows from two tables where a matching column has the same value.

Syntax:

... JOIN table2 ON table1.col = table2.col;

Example:

SELECT c.name, o.amount FROM Customers c JOIN Orders o ON c.id = o.customer_id;

GROUP BY / COUNT

Definition: GROUP BY collects rows with the same value; aggregate functions such as COUNT, SUM and AVG calculate one result per group.

Syntax:

SELECT col, COUNT(*) FROM table GROUP BY col;

Example:

SELECT country, COUNT(*) FROM Customers GROUP BY country;

Customers

Definition: All lessons use two sample tables: Customers (id, name, city, country) with Ada, Bob, Chen, Dev and Eva, and Orders (id, customer_id, amount) with four orders.

Syntax:

Customers

Example:

SELECT * FROM Customers;

↗ SQL introduction

ASC

Definition: ORDER BY sorts ascending ( ASC , the default) or descending ( DESC ).

Syntax:

ASC

Example:

ORDER BY country ASC, name DESC

↗ SQL ORDER BY and LIMIT

DESC

Definition: ORDER BY sorts ascending ( ASC , the default) or descending ( DESC ).

Syntax:

DESC

Example:

ORDER BY country ASC, name DESC

↗ SQL ORDER BY and LIMIT

AND

Definition: AND needs both conditions true, OR needs one, NOT reverses a condition.

Syntax:

AND

Example:

AND NOT city = 'London';

↗ SQL AND, OR, NOT

OR

Definition: AND needs both conditions true, OR needs one, NOT reverses a condition.

Syntax:

OR

Example:

WHERE (country = 'UK' OR country = 'India')

↗ SQL AND, OR, NOT

NOT

Definition: AND needs both conditions true, OR needs one, NOT reverses a condition.

Syntax:

NOT

Example:

AND NOT city = 'London';

↗ SQL AND, OR, NOT

NULL

Definition: A column you skip gets NULL , meaning no value.

Syntax:

NULL

Example:

WHERE city IS NULL;

↗ SQL INSERT INTO and NULL

MIN

Definition: Aggregate functions turn many rows into one value: MIN , MAX , COUNT , SUM and AVG .

Syntax:

MIN

Example:

SELECT MIN(amount) AS lowest,

↗ SQL aggregate functions

MAX

Definition: Aggregate functions turn many rows into one value: MIN , MAX , COUNT , SUM and AVG .

Syntax:

MAX

Example:

MAX(amount) AS highest,

↗ SQL aggregate functions

COUNT

Definition: Aggregate functions turn many rows into one value: MIN , MAX , COUNT , SUM and AVG .

Syntax:

COUNT

Example:

COUNT(*)    AS orders,

↗ SQL aggregate functions

SUM

Definition: Aggregate functions turn many rows into one value: MIN , MAX , COUNT , SUM and AVG .

Syntax:

SUM

Example:

SUM(amount) AS total,

↗ SQL aggregate functions

AVG

Definition: Aggregate functions turn many rows into one value: MIN , MAX , COUNT , SUM and AVG .

Syntax:

AVG

Example:

AVG(amount) AS average

↗ SQL aggregate functions

LIKE

Definition: LIKE matches patterns.

Syntax:

LIKE

Example:

WHERE name LIKE '%a';

↗ SQL LIKE and wildcards

IN

Definition: IN is a short way to write many OR tests.

Syntax:

IN

Example:

WHERE country IN ('UK', 'India');

↗ SQL IN and BETWEEN

AS

Definition: AS renames a column in the result, or a table inside the query.

Syntax:

AS

Example:

SELECT name AS customer, city AS town

↗ SQL aliases

UNION

Definition: UNION joins the results of two SELECT s and removes duplicates.

Syntax:

UNION

Example:

UNION

↗ SQL UNION and UNION ALL

HAVING

Definition: WHERE filters rows before grouping; HAVING filters the groups after .

Syntax:

HAVING

Example:

HAVING COUNT(*) > 1;

↗ SQL GROUP BY and HAVING

CASE

Definition: CASE returns a value depending on conditions, like if / else if / else .

Syntax:

CASE

Example:

CASE

↗ SQL CASE

EXISTS

Definition: EXISTS is true when the subquery returns at least one row.

Syntax:

EXISTS

Example:

WHERE EXISTS (

↗ SQL EXISTS, ANY and ALL

UNIQUE

Definition: Constraints keep data valid: NOT NULL , UNIQUE , PRIMARY KEY (unique row id), FOREIGN KEY (link to another table), CHECK and DEFAULT .

Syntax:

UNIQUE

Example:

name VARCHAR(50) NOT NULL UNIQUE,

↗ SQL CREATE TABLE and constraints

CHECK

Definition: Constraints keep data valid: NOT NULL , UNIQUE , PRIMARY KEY (unique row id), FOREIGN KEY (link to another table), CHECK and DEFAULT .

Syntax:

CHECK

Example:

price DECIMAL(8,2) CHECK (price >= 0),

↗ SQL CREATE TABLE and constraints

DEFAULT

Definition: Constraints keep data valid: NOT NULL , UNIQUE , PRIMARY KEY (unique row id), FOREIGN KEY (link to another table), CHECK and DEFAULT .

Syntax:

DEFAULT

Example:

stock INT DEFAULT 0

↗ SQL CREATE TABLE and constraints

AUTO_INCREMENT

Definition: AUTO_INCREMENT numbers new rows for you (MySQL).

Syntax:

AUTO_INCREMENT

Example:

id INT AUTO_INCREMENT PRIMARY KEY,

↗ SQL CREATE TABLE and constraints

Example code

SELECT * FROM Customers;

Run this code in the online editor

← Welcome to the SQL courseSQL introduction →