SQL Basics
SQL Basics
SELECT DISTINCT column1, Used to return only SELECT DISTINCT Country FROM Customers;
column2, distinct (different)
FROM table_name; values
SELECT column1, column2, ... WHERE clause is used to SELECT * FROM Customers
FROM table_name extract only those WHERE Country='Mexico';
WHERE condition; records that fulfill a
specified condition
SELECT column1, column2, ... WHERE clause can be SELECT * FROM Customers
FROM table_name combined with AND, OR, WHERE Country='Germany' AND City='Berlin';
WHERE condition1 AND/NOT/OR and NOT operators.
condition2 AND/NOT/OR condition3 SELECT * FROM Customers
...; The AND and OR operators WHERE Country='Germany' AND (City='Berlin'
are used to filter OR City='München');
records based on more
than one condition
SELECT column_name(s) FROM JOINS values from two or SELECT City FROM Customers
table1 more tables UNION ALL
UNION SELECT City FROM Suppliers
SELECT column_name(s) FROM ORDER BY City;
table2;
SELECT column_name(s) The GROUP BY statement SELECT COUNT(CustomerID), Country
FROM table_name is often used with FROM Customers
WHERE condition aggregate functions GROUP BY Country;
GROUP BY column_name(s) (COUNT, MAX, MIN, SUM,
ORDER BY column_name(s); AVG) to group the
result-set by one or
more columns.
INSERT INTO table2 The INSERT INTO SELECT INSERT INTO Customers (CustomerName, City,
SELECT * FROM table1 statement copies data Country)
WHERE condition; from one table and SELECT SupplierName, City, Country FROM
inserts it into another Suppliers;
table.
INSERT INTO
SELECT requires
that data types
in source and
target tables
match
The existing
records in the
target table are
unaffected
Left Join: