Joins in SQL
Joins in SQL
Joins is used to combine rows from two or more tables, based on a related column between them.
(INNER) JOIN:
Returns records that have matching values in both tables
Syntax
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
LEFT (OUTER) JOIN:
Returns all records from the left table, and the matched records from the right table
SELECT column_name(s)
WHERE condition;
The SQL UNION Operator
The UNION operator is used to combine the result-set of two or more SELECT statements.
UNION Syntax
SELECT column_name(s) FROM table1
UNION
GROUP BY Statement
The GROUP BY statement groups rows that have the same values into summary rows, like "find the
number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG())
to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
The ALL operator:
returns a boolean value as a result
FROM table_name
(SELECT column_name
FROM table_name
WHERE condition);
The SQL SELECT INTO Statement
The SELECT INTO statement copies data from one table into a new table.
SELECT *
FROM oldtable
WHERE condition;
Single Line Comments
Single line comments start with --.
Any text between -- and the end of the line will be ignored (will not be executed)
If you define a CHECK constraint on a column it will allow only certain values for this column.