Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 83

Introduction to SQL SELECT statement

The SQL SELECT statement selects data from one or more tables. The following
shows the basic syntax of the SELECT statement that selects data from a single
table.

SELECT
select_list
FROM
table_name;
Code language: SQL (Structured Query Language) (sql)

In this syntax:

 First, specify a list of comma-separated columns from the table in


the SELECT clause.
 Then, specify the table name in the FROM clause.

When evaluating the SELECT statement, the database system evaluates


the FROM clause first and then the SELECT clause. It’s like from a table, select
data from these columns.

The semicolon (;) is not part of a query. The database server uses it to separate two
SQL statements.

For example, if you execute two SQL SELECT statements, you need to separate
them using the semicolon (;).

If you want to query data from all the columns of the table, you can use the asterisk
(*) operator instead if specifying all the column names:

SELECT * FROM table_name;

SQL is case-insensitive. Therefore, the SELECT and select keywords have the
same meaning.

By convention, we will use the uppercase letters for the SQL keywords, such
as SELECT and FROM and the lowercase letters for the identifiers such as table
and column names. This convention makes the SQL statements more readable.
1) SQL SELECT – selecting data from all columns example

The following example uses the SQL SELECT statement to get data from all the
rows and columns in the employees table:

SELECT * FROM employees;

2) SQL SELECT – selecting data from specific columns

To select data from specific columns, you can specify the column list after
the SELECT clause of the SELECT statement.

For example, the following select data from the employee id, first name, last name,
and hire date of all rows in the employees table:

SELECT
employee_id,
first_name,
last_name,
hire_date
FROM
employees;

3) SQL SELECT – performing a simple calculation

The following example uses the SELECT statement to get the first name, last
name, salary, and new salary:

SELECT
first_name,
last_name,
salary,
salary * 1.05
FROM
employees;
Code language: SQL (Structured Query Language) (sql)

The expression salary * 1.05 adds 5% to the salary of every employee. By default,
SQL uses the expression as the column heading:
To assign an expression or a column an alias, you specify the AS keyword
followed by the column alias as follows:

expression AS column_alias
Code language: SQL (Structured Query Language) (sql)

For example, the following SELECT statement uses the new_salary as the column
alias for the salary * 1.05 expression:

SELECT
first_name,
last_name,
salary,
salary * 1.05 AS new_salary
FROM
employees;
Code language: SQL (Structured Query Language) (sql)

Output:
Introduction to SQL ORDER BY clause

The ORDER BY is an optional clause of the SELECT statement. The ORDER


BY clause allows you to sort the rows returned by the SELECT clause by one or
more sort expressions in ascending or descending order.

The following shows the syntax of the ORDER BY clause:

SELECT
select_list
FROM
table_name
ORDER BY
sort_expression [ASC | DESC];
Code language: SQL (Structured Query Language) (sql)

In this syntax:
 First, place the ORDER BY clause after the FROM clause. The database
will evaluate the SELECT statement with the ORDER BY * clause in the
following order: FROM > SELECT > ORDER BY.
 Second, specify a sort expression after the ORDER BY clause. The sort
expression specifies the sort criteria.
 Third, use ASC option to sort the result set by the sort expression in
ascending order and DESC to sort the result set by the sort expression in the
descending order.

Note that the ORDER BY clause uses the ASC option by default if you don’t
either ASC or DESC.

The ORDER BY clause also allows you to sort the result set by multiple
expressions. In this case, you need to use a comma to separate two sort
expressions:

SELECT
select_list
FROM
table_name
ORDER BY
sort_expression_1 [ASC | DESC],
sort_expression_2 [ASC | DESC];
Code language: CSS (css)

In this syntax, the ORDER BY clause sorts the result set by


the sort_expression_1 first, and then sorts the sorted result set by
the sort_expression_2.

Note that if you don’t specify the ORDER BY clause, the SELECT statement will
not sort the result set. It means that the rows in the result set don’t have a specific
order.

Introduction to SQL DISTINCT operator

To remove duplicate rows from a result set, you use the DISTINCT operator in
the SELECT clause as follows:

SELECT DISTINCT
column1, column2, ...
FROM
table1;
Code language: SQL (Structured Query Language) (sql)

If you use one column after the DISTINCT operator, the DISTINCT operator uses
values in that column to evaluate duplicates.

If you use two or more columns, the DISTINCT will use the combination of values
in those columns to evaluate the duplicate.

Note that the DISTINCT only removes the duplicate rows from the result set. It
doesn’t delete duplicate rows in the table.

If you want to select two columns and remove duplicates in one column, you
should use the GROUP BY clause instead.

1) Using SQL DISTINCT operator on one column example

The following statement selects the salary data from the salary column of
the employees table and sorts them from high to low:

SELECT
salary
FROM
employees
ORDER BY salary DESC;
2) Using SQL DISTINCT operator on multiple columns example

The following statement selects the job id and salary from the employees table:

SELECT
job_id,
salary
FROM
employees
ORDER BY
job_id,
salary DESC;
SQL DISTINCT and NULL

In the database, NULL means unknown or missing data.

Unlike values like numbers, strings, dates, etc. NULL does not equal anything,
even itself. The following expression will return unknown (or NULL):

NULL=NULL
Code language: PHP (php)

Typically, the DISTINCT operator treats all NULL the same. Therefore,
the DISTINCT operator keeps only one NULL in the result set.

Note that this behavior may be different between database products.

For example, the following statement returns the distinct phone numbers of
employees:

SELECT DISTINCT phone_number


FROM employees
ORDER BY phone_number;

Introduction to SQL WHERE clause

To select specific rows from a table, you use a WHERE clause in


the SELECT statement. The following illustrates the syntax of the WHERE clause
in the SELECT statement:

SELECT
column1, column2, ...
FROM
table_name
WHERE
condition;
Code language: SQL (Structured Query Language) (sql)

The WHERE clause appears immediately after


the FROM clause. The WHERE clause contains one or more logical expressions
that evaluate each row in the table. If a row that causes the condition evaluates to
true, it will be included in the result set; otherwise, it will be excluded.
Note that SQL has three-valued logic which are TRUE, FALSE, and UNKNOWN.
It means that if a row causes the condition to evaluate to FALSE or NULL, the row
will not be returned.

Note that the logical expression that follows the WHERE clause is also known as a
predicate. You can use various operators to form the row selection criteria used in
the WHERE clause.

The following table shows the SQL comparison operators:

Operator Meaning

= Equal to

<> (!=) Not equal to

< Less than

> Greater than

<= Less than or equal

>= Greater than or equal

To form a simple expression, you use one of the operators above with two
operands that can be either column name on one side and a literal value on the
other, for example:

salary > 1000


Code language: SQL (Structured Query Language) (sql)

It asks a question: “Is salary greater than 1000?”.

Or you can use column names on both sides of an operator such as:

min_salary < max_salary


Code language: SQL (Structured Query Language) (sql)
This expression asks another question: “Is the min salary less than the max
salary?”.

The literal values that you use in an expression can be numbers, characters, dates,
and times, depending on the format you use:

 Number: use a number that can be an integer or a decimal without any


formatting e.g., 100, 200.5
 Character: use characters surrounded by either single or double quotes e.g.,
“100”, “John Doe”.
 Date: use the format that the database stores. It depends on the database
system e.g., MySQL uses 'yyyy-mm-dd' format to store the date data.
 Time: use the format that the database system uses to store the time. For
example, MySQL uses 'HH:MM:SS' to store time data.

Besides the SELECT statement, you can use the WHERE clause in
the UPDATE or DELETE statement to specify which rows to be updated or
deleted.

SQL WHERE clause with numeric comparison examples

The following query finds employees who have salaries greater than 14,000
and sorts the result set based on the salary in descending order.

SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary > 14000
ORDER BY
salary DESC;
SQL WHERE clause with characters example

SQL is case-insensitive. However, when it comes to the values in the comparisons,


it is case-sensitive. For instance, the following query finds the employee whose last
name is Chen.
SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
last_name = 'Chen';
SQL WHERE clause with dates examples

To get all employees who joined the company after January 1st, 1999, you use the
following query:

SELECT
employee_id,
first_name,
last_name,
hire_date
FROM
employees
WHERE
hire_date >= '1999-01-01'
ORDER BY
hire_date DESC;

SQL Comparison Operators

The SQL comparison operators allow you to test if two expressions are the same.
The following table illustrates the comparison operators in SQL:

Operator Meaning

= Equal

<> Not equal to

> Greater than


Operator Meaning

>= Greater than or equal to

< Less than

<= Less than or equal to

The result of a comparison operator has one of three value true, false, and
unknown.

Equal to operator(=)

The equal to operator compares the equality of two expressions:

expression1 = expression2
Code language: SQL (Structured Query Language) (sql)

It returns true if the value of the left expression is equal to the value of the right
expression; otherwise, it returns false.

For example, the following statement finds the employee whose last name
is Himuro:

SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
last_name = 'Himuro';
Code language: SQL (Structured Query Language) (sql)

In this example, the query searches for the string Himuro in the last_name column
of the employees table.
Note that the equal operator cannot be used to compare null values. For example,
the intention of the following query is to find all employees who do not have phone
numbers:

SELECT
employee_id, first_name, last_name, phone_number
FROM
employees
WHERE
phone_number = NULL;
Code language: SQL (Structured Query Language) (sql)

However, it returns an empty result set because the following expression always
returns false.

phone_number = NULL
Code language: SQL (Structured Query Language) (sql)

To compare null values, you use the IS NULL operator instead:

SELECT
employee_id, first_name, last_name, phone_number
FROM
employees
WHERE
phone_number IS NULL;
Code language: SQL (Structured Query Language) (sql)

Not equal to operator (<>)

The not equal to (<>) operator compares two non-null expressions and returns true
if the value of the left expression is not equal to the right one; otherwise, it returns
false.
expression1 <> expression2
Code language: SQL (Structured Query Language) (sql)

For example, the following statement returns all employees whose department id is
not 8.

SELECT
employee_id, first_name, last_name, department_id
FROM
employees
WHERE
department_id <> 8
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

You can use the AND operator to combine multiple expressions that use the not
equal to (<>) operator. For example, the following statement finds all employees
whose department id is not eight and ten.

SELECT
employee_id, first_name, last_name, department_id
FROM
employees
WHERE
department_id <> 8
AND department_id <> 10
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)
Greater than operator (>)

The greater than operator (>) compares two non-null expressions and returns true if
the left operand is greater than the right operand; otherwise, the result is false.

expression1 > expression2


Code language: SQL (Structured Query Language) (sql)

For example, to find the employees whose salary is greater than 10,000, you use
the greater than operator in the WHERE clause as follows:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary > 10000
ORDER BY salary DESC;
Code language: SQL (Structured Query Language) (sql)
You can combine expressions that use various comparison operators using
the AND or OR operator. For example, the following statement finds employees in
department 8 and have the salary greater than 10,000:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary > 10000 AND department_id = 8
ORDER BY salary DESC;
Code language: SQL (Structured Query Language) (sql)

Less than operator (<)

The less than operator compares two non-null expressions. The result is true if the
left operand evaluates to a value that is lower than the value of the right operand;
otherwise, the result is false.

The following shows the syntax of the less than operator:

expression1 < expression2


Code language: SQL (Structured Query Language) (sql)

For example, the statement below returns all employees whose salaries are less
than 10,000:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary < 10000
ORDER BY salary DESC;
Code language: SQL (Structured Query Language) (sql)
Greater than or equal operator (>=)

The greater than or equal operator (>=) compares two non-null expressions. The
result is true if the left expression evaluates to a value that is greater than the value
of the right expression.

The following illustrates the syntax of the greater than or equal operator:

expression1 >= expression2


Code language: SQL (Structured Query Language) (sql)

For example, the following statement finds employees whose salaries are greater
than or equal 9,000:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary >= 9000
ORDER BY salary;
Code language: SQL (Structured Query Language) (sql)
Less than or equal to operator(<=)

The less than or equal to operator compares two non-null expressions and returns
true if the left expression has a value less than or equal the value of the right
expression; otherwise, it returns true.

The following shows the syntax of the less than or equal to operator:

expression1 <= expression2


Code language: SQL (Structured Query Language) (sql)

For example, the following statement finds employees whose salaries are less than
or equal to 9,000:

SQL Logical Operators

A logical operator allows you to test for the truth of a condition. Similar to
a comparison operator, a logical operator returns a value of true, false, or unknown.

The following table illustrates the SQL logical operators:


Operator Meaning

ALL Return true if all comparisons are true

AND Return true if both expressions are true

ANY Return true if any one of the comparisons is true.

BETWEEN Return true if the operand is within a range

EXISTS Return true if a subquery contains any rows

IN Return true if the operand is equal to one of the value in a list

LIKE Return true if the operand matches a pattern

NOT Reverse the result of any other Boolean operator.

OR Return true if either expression is true

SOME Return true if some of the expressions are true

AND

The AND operator allows you to construct multiple conditions in


the WHERE clause of an SQL statement such as SELECT, UPDATE,
and DELETE:

expression1 AND expression2


Code language: SQL (Structured Query Language) (sql)

The AND operator returns true if both expressions evaluate to true.

The following example finds all employees whose salaries are greater than 5,000
and less than 7,000:

SELECT
first_name, last_name, salary
FROM
employees
WHERE
salary > 5000 AND salary < 7000
ORDER BY salary;
Code language: SQL (Structured Query Language) (sql)

OR

Similar to the AND operator, the OR operator combines multiple conditions in an


SQL statement’s WHERE clause:

expression1 OR expression2
Code language: SQL (Structured Query Language) (sql)

However, the OR operator returns true if a least one expression evaluates to true.

For example, the following statement finds employees whose salary is either 7,000
or 8,000:

SELECT
first_name, last_name, salary
FROM
employees
WHERE
salary = 7000 OR salary = 8000
ORDER BY salary;
Code language: SQL (Structured Query Language) (sql)

IS NULL
The IS NULL operator compares a value with a null value and returns true if the
compared value is null; otherwise, it returns false.

For example, the following statement finds all employees who do not have a phone
number:

SELECT
first_name, last_name, phone_number
FROM
employees
WHERE
phone_number IS NULL
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

BETWEEN

The BETWEEN operator searches for values that are within a set of values, given
the minimum value and maximum value. Note that the minimum and maximum
values are included as part of the conditional set.

For example, the following statement finds all employees whose salaries are
between 9,000 and 12,000.

SELECT
first_name, last_name, salary
FROM
employees
WHERE
salary BETWEEN 9000 AND 12000
ORDER BY salary;
Code language: SQL (Structured Query Language) (sql)
Notice that the value 9,000 and 12,000 are included in the output.

IN

The IN operator compares a value to a list of specified values. The IN operator


returns true if the compared value matches at least one value in the list; otherwise,
it returns false.

The following statement finds all employees who work in the department id 8 or 9.

SELECT
first_name, last_name, department_id
FROM
employees
WHERE
department_id IN (8, 9)
ORDER BY department_id;
Code language: SQL (Structured Query Language) (sql)

LIKE

The LIKE operator compares a value to similar values using a wildcard operator.
SQL provides two wildcards used in conjunction with the LIKE operator:
 The percent sign ( %) represents zero, one, or multiple characters.
 The underscore sign ( _) represents a single character.

The following statement finds all employees whose first name starts with the
string jo:

SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
first_name LIKE 'jo%'
ORDER BY first_name;
Code language: SQL (Structured Query Language) (sql)

The following example finds all employees with the first names whose the second
character is h:

SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
first_name LIKE '_h%'
ORDER BY first_name;
Code language: SQL (Structured Query Language) (sql)

ALL
The ALL operator compares a value to all values in another value set.
The ALL operator must be preceded by a comparison operator and followed by
a subquery.

The following illustrates the syntax of the ALL operator:

comparison_operator ALL (subquery)


Code language: SQL (Structured Query Language) (sql)

Note that you will learn about the subquery in the subquery tutorial.

The following example finds all employees whose salaries are greater than all
salaries of employees in the department 8:

SELECT
first_name, last_name, salary
FROM
employees
WHERE
salary >= ALL (SELECT
salary
FROM
employees
WHERE
department_id = 8)
ORDER BY salary DESC;
Code language: SQL (Structured Query Language) (sql)

ANY

The ANY operator compares a value to any value in a set according to the
condition as shown below:

comparison_operator ANY(subquery)
Code language: SQL (Structured Query Language) (sql)
Similar to the ALL operator, the ANY operator must be preceded by a comparison
operator and followed by a subquery.

For example, the following statement finds all employees whose salaries are
greater than the average salary of every department:

SELECT
first_name, last_name, salary
FROM
employees
WHERE
salary > ANY(SELECT
AVG(salary)
FROM
employees
GROUP BY department_id)
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

Note that SOME is an alias for ANY, therefore, you can use them interchangeably.

EXISTS

The EXISTS operator tests if a subquery contains any rows:

EXISTS (subquery)
Code language: SQL (Structured Query Language) (sql)

If the subquery returns one or more rows, the result of the EXISTS is true;
otherwise, the result is false.
For example, the following statement finds all employees who have dependents:

SELECT
first_name, last_name
FROM
employees e
WHERE
EXISTS( SELECT
1
FROM
dependents d
WHERE
d.employee_id = e.employee_id);
Code language: SQL (Structured Query Language) (sql)

Introduction to SQL IN Operator

The IN is a logical operator in SQL. The IN operator returns true if a value is in a


set of values or false otherwise.

The following illustrates the syntax of the IN operator:

expression IN (value1,value2,...)
Code language: SQL (Structured Query Language) (sql)

Technically, you can substitute the IN operator with the = and OR operators The
condition that uses the IN operator can be rewritten using one or
more OR operators as follows:

expression = value1 OR expression = value2 OR ...


Code language: SQL (Structured Query Language) (sql)
To negate the IN operator, you use the NOT operator:

expression NOT IN (value1, value2,...)


Code language: SQL (Structured Query Language) (sql)

The NOT IN operator returns true if the expression does not equal any values in
the list or false otherwise.

To substitute the IN operator, you can use the != and AND operators as follows:

expression != value1 AND expression != value2 AND...


Code language: SQL (Structured Query Language) (sql)

Notice that if any value in the list (value1,value2,...) is null, the IN operator returns
no rows.

In practice, you often use the IN and NOT IN operators in the WHERE clause of
the SELECT statement to select rows with a value in a set of values. Also, you’ll
use the IN operator in subqueries.

The following example uses the IN operator to find employees with the job id is 8,
9, or 10:

SELECT
employee_id,
first_name,
last_name,
job_id
FROM
employees
WHERE
job_id IN (8, 9, 10)
ORDER BY
job_id;

The following example uses the NOT IN operator to find employees whose
job’s id is neither 7, 8, nor 9:

SELECT
employee_id,
first_name,
last_name,
job_id
FROM
employees
WHERE
job_id NOT IN (7, 8, 9)
ORDER BY
job_id;
2) Using SQL IN opeator with a subquery example

A subquery is a query nested inside another query. Let’s take a look at an example:

The following query returns the department id of


the Marketing and Sales departments:

SELECT
department_id
FROM
departments
WHERE
department_name = 'Marketing'
OR department_name = 'Sales'

The query returns a list of two department ids.

And you can pass the id list to the IN operator to find employees who work in
the Marketing and Sales departments like this:
SELECT
employee_id,
first_name,
last_name,
department_id
FROM
employees
WHERE
department_id IN (2, 8);

To combine two above queries into a single query, you can use the first query in
place of the list inside parentheses followed the IN operator:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
department_id IN (SELECT
department_id
FROM
departments
WHERE
department_name = 'Marketing'
OR department_name = 'Sales')

Introduction to SQL LIKE operator

The LIKE operator is one of the SQL logical operators. The LIKE operator returns
true if a value matches a pattern or false otherwise.

The syntax of the LIKE operator is as follows:

expression LIKE pattern


Code language: SQL (Structured Query Language) (sql)

In this syntax, the LIKE operator tests whether an expression matches the pattern.
The SQL standard provides you with two wildcard characters to make a pattern:

 % percent wildcard matches zero, one, or more characters


 _ underscore wildcard matches a single character.
The following show an example of using the % and _ wildcard characters:

Expression Meaning

LIKE 'Kim%' match a string that starts with Kim

LIKE '%er' match a string that ends with er

LIKE '%ch%' match a string that contains ch

LIKE 'Le_' match a string that starts with Le and is followed by one character e.g., Les,
Len…

LIKE '_uy' match a string that ends with uy and is preceded by one character e.g., guy

LIKE '%are_' match a string that contains are and ends with one character

LIKE '_are%' match a string that contains are, starts with one character, and ends with any
number of characters

Note that besides the % and _ wildcards, some database systems may have other
wildcard characters that are specific to those databases.

NOT LIKE

To negate the LIKE operator, you use the NOT operator:

expression NOT LIKE pattern

The NOT LIKE operator returns true if the expression doesn’t match the pattern or
false otherwise.

Escape character

To match a string that contains a wildcard for example 10%, you need to instruct
the LIKE operator to treat the % in 10% as a regular character.
To do that, you need to explicitly specify an escape character after
the ESCAPE clause:

expression LIKE pattern ESCAPE escape_character


Code language: SQL (Structured Query Language) (sql)

For example:

value LIKE '%10!%%' ESCAPE '!'


Code language: JavaScript (javascript)

In this example, the ! is an escape character. It instructs the LIKE operator to treat
the % in the 10% as a regular character.

In practice, you often use the LIKE operator in WHERE clause of


the SELECT, UPDATE, and DELETE statements.

The following example uses the LIKE operator to find all employees whose first
names start with Da :

SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
first_name LIKE 'Da%';

The following example use the LIKE operator to find all employees whose first
names end with er:

SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
first_name LIKE '%er';
The following example uses the LIKE operator to find employees whose last
names contain the word an:

SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
last_name LIKE '%an%';

The following statement retrieves employees whose first names start with Jo and
are followed by at most 2 characters:

SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
first_name LIKE 'Jo__';

The following statement uses the LIKE operator with the % and _ wildcard to find
employees whose first names start with any number of characters and are followed
by at most one character:

SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
first_name LIKE '%are_';
SQL NOT LIKE operator example

The following example uses the NOT LIKE operator to find all employees whose
first names start with the letter S but not start with Sh:
SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
first_name LIKE 'S%'
AND first_name NOT LIKE 'Sh%'
ORDER BY
first_name;

Introduction to the SQL NOT operator

You have learned how to use various logical operators such


as AND, OR, LIKE, BETWEEN, IN, and EXISTS. These operators help you to
form flexible conditions in the WHERE clause.

To negate the result of any Boolean expression, you use the NOT operator. The
following illustrates how to use the NOT operator:

NOT [Boolean_expression]
Code language: SQL (Structured Query Language) (sql)

The following table shows the result of the NOT operator.

NOT

TRUE FALSE

FALSE TRUE

NULL NULL

To get the employees who work in the department id 5 and with a salary not
greater than 5000.

SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
department_id = 5
AND NOT salary > 5000
ORDER BY
salary;
SQL NOT with IN operator example

To negate the IN operator, you use the NOT operator. For example, the following
statement gets all the employees who are not working in the departments 1, 2, or 3.

SELECT
employee_id,
first_name,
last_name,
department_id
FROM
employees
WHERE
department_id NOT IN (1, 2, 3)
ORDER BY
first_name;
SQL NOT LIKE operator example

You can negate the LIKE operator by using the NOT LIKE. For example, the
following statement retrieves all the employees whose first names do not start with
the letter D.

SELECT
first_name,
last_name
FROM
employees
WHERE
first_name NOT LIKE 'D%'
ORDER BY
first_name;
SQL NOT BETWEEN example

The following example shows you how to use the NOT to negate
the BETWEEN operator to get employees whose salaries are not between 5,000
and 1,000.

SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary NOT BETWEEN 3000
AND 5000
ORDER BY
salary;
SQL NOT EXISTS example

See the following employees and dependents tables:

The following query uses the NOT EXISTS operator to get the employees who do
not have any dependents.
SELECT
employee_id,
first_name,
last_name
FROM
employees e
WHERE
NOT EXISTS (
SELECT
employee_id
FROM
dependents d
WHERE
d.employee_id = e.employee_id
);

SQL IS NULL

What is NULL

NULL is special in SQL. NULL indicates that the data is unknown, inapplicable,
or even does not exist. In other words, NULL represents the missing data in the
database.

For example, if employees do not have phone numbers, you can store their phone
numbers as empty strings.

However, if you don’t know their phone numbers when you save the employee
records, you need to use the NULL for the unknown phone numbers.

The NULL is special because any comparisons with a NULL can never result in
true or false, but in a third logical result, unknown.

The following statement returns NULL:

SELECT NULL = 5;

You cannot use the comparison operator equal to (=) to compare a value to
a NULL value. For example, the following statement will not return the
correct result:
SELECT
employee_id,
first_name,
last_name,
phone_number
FROM
employees
WHERE
phone_number = NULL;

The IS NULL and IS NOT NULL operators

To determine whether an expression or column is NULL or not, you use the IS


NULL operator as follows:

expression IS NULL
Code language: SQL (Structured Query Language) (sql)

If the result of the expression is NULL, IS NULL operator returns true; otherwise,
it returns false.

To check if an expression or column is not NULL, you use the IS NOT operator:

expression IS NOT NULL


Code language: PHP (php)

The IS NOT NULL returns false if the value of the expression is NULl; otherwise,
it returns true;

SQL Subquery

SQL subquery basic

Consider the following employees and departments tables from the sample
database:
Suppose you have to find all employees who locate in the location with the id
1700. You might come up with the following solution.

First, find all departments located at the location whose id is 1700:

SELECT
*
FROM
departments
WHERE
location_id = 1700;
Code language: SQL (Structured Query Language) (sql)

Second, find all employees that belong to the location 1700 by using the
department id list of the previous query:

SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
department_id IN (1 , 3, 8, 10, 11)
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

This solution has two problems. To start with, you have looked at
the departments table to check which department belongs to the location 1700.
However, the original question was not referring to any specific departments; it
referred to the location 1700.

Because of the small data volume, you can get a list of department easily.
However, in the real system with high volume data, it might be problematic.

Another problem was that you have to revise the queries whenever you want to
find employees who locate in a different location.

A much better solution to this problem is to use a subquery. By definition, a


subquery is a query nested inside another query such
as SELECT, INSERT, UPDATE, or DELETE statement. In this tutorial, we are
focusing on the subquery used with the SELECT statement.

In this example, you can rewrite combine the two queries above as follows:

SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
department_id IN (SELECT
department_id
FROM
departments
WHERE
location_id = 1700)
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

The query placed within the parentheses is called a subquery. It is also known as
an inner query or inner select. The query that contains the subquery is called an
outer query or an outer select.

To execute the query, first, the database system has to execute the subquery and
substitute the subquery between the parentheses with its result – a number of
department id located at the location 1700 – and then executes the outer query.

You can use a subquery in many places such as:

 With the IN or NOT IN operator


 With comparison operators
 With the EXISTS or NOT EXISTS operator
 With the ANY or ALL operator
 In the FROM clause
 In the SELECT clause

SQL subquery examples

Let’s take some examples of using the subqueries to understand how they work.

SQL subquery with the IN or NOT IN operator

In the previous example, you have seen how the subquery was used with
the IN operator. The following example uses a subquery with the NOT IN operator
to find all employees who do not locate at the location 1700:

SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
department_id NOT IN (SELECT
department_id
FROM
departments
WHERE
location_id = 1700)
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

SQL subquery with the comparison operator

The following syntax illustrates how a subquery is used with a comparison


operator:

comparison_operator (subquery)
Code language: SQL (Structured Query Language) (sql)

where the comparison operator is one of these operators:

 Equal (=)
 Greater than (>)
 Less than (<)
 Greater than or equal ( >=)
 Less than or equal (<=)
 Not equal ( !=) or (<>)

The following example finds the employees who have the highest salary:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary = (SELECT
MAX(salary)
FROM
employees)
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

In this example, the subquery returns the highest salary of all employees and the
outer query finds the employees whose salary is equal to the highest one.

The following statement finds all employees who salaries are greater than the
average salary of all employees:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary > (SELECT
AVG(salary)
FROM
employees);
Code language: SQL (Structured Query Language) (sql)
In this example, first, the subquery returns the average salary of all employees.
Then, the outer query uses the greater than operator to find all employees whose
salaries are greater than the average.

SQL subquery with the EXISTS or NOT EXISTS operator

The EXISTS operator checks for the existence of rows returned from the subquery.
It returns true if the subquery contains any rows. Otherwise, it returns false.

The syntax of the EXISTS operator is as follows:

EXISTS (subquery )
Code language: SQL (Structured Query Language) (sql)

The NOT EXISTS operator is opposite to the EXISTS operator.

NOT EXISTS (subquery)


Code language: SQL (Structured Query Language) (sql)

The following example finds all departments which have at least one employee
with the salary is greater than 10,000:

SELECT
department_name
FROM
departments d
WHERE
EXISTS( SELECT
1
FROM
employees e
WHERE
salary > 10000
AND e.department_id = d.department_id)
ORDER BY department_name;
Code language: SQL (Structured Query Language) (sql)
Similarly, the following statement finds all departments that do not have any
employee with the salary greater than 10,000:

SELECT
department_name
FROM
departments d
WHERE
NOT EXISTS( SELECT
1
FROM
employees e
WHERE
salary > 10000
AND e.department_id = d.department_id)
ORDER BY department_name;
Code language: SQL (Structured Query Language) (sql)

SQL subquery with the ALL operator

The syntax of the subquery when it is used with the ALL operator is as follows:

comparison_operator ALL (subquery)


Code language: SQL (Structured Query Language) (sql)

The following condition evaluates to true if x is greater than every value returned
by the subquery.
x > ALL (subquery)
Code language: SQL (Structured Query Language) (sql)

For example, suppose the subquery returns three value one, two, and three. The
following condition evaluates to true if x is greater than 3.

x > ALL (1,2,3)


Code language: SQL (Structured Query Language) (sql)

The following query uses the GROUP BY clause and MIN() function to find the
lowest salary by department:

SELECT
MIN(salary)
FROM
employees
GROUP BY department_id
ORDER BY MIN(salary) DESC;
Code language: SQL (Structured Query Language) (sql)

The following example finds all employees whose salaries are greater than the
lowest salary of every department:

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary >= ALL (SELECT
MIN(salary)
FROM
employees
GROUP BY department_id)
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

SQL subquery with the ANY operator

The following shows the syntax of a subquery with the ANY operator:

comparison_operator ANY (subquery)


Code language: SQL (Structured Query Language) (sql)

For example, the following condition evaluates to true if x is greater than any value
returned by the subquery. So the condition x > SOME (1,2,3) evaluates to true if x
is greater than 1.

x > ANY (subquery)


Code language: SQL (Structured Query Language) (sql)

Note that the SOME operator is a synonym for the ANY operator so you can use
them interchangeably.

The following query finds all employees whose salaries are greater than or equal to
the highest salary of every department.

SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary >= SOME (SELECT
MAX(salary)
FROM
employees
GROUP BY department_id);
Code language: SQL (Structured Query Language) (sql)

In this example, the subquery finds the highest salary of employees in each
department. The outer query looks at these values and determines which
employee’s salaries are greater than or equal to any highest salary by department.

SQL subquery in the FROM clause

You can use a subquery in the FROM clause of the SELECT statement as follows:

SELECT
*
FROM
(subquery) AS table_name
Code language: SQL (Structured Query Language) (sql)

In this syntax, the table alias is mandatory because all tables in the FROM clause
must have a name.

Note that the subquery specified in the FROM clause is called a derived table in
MySQL or inline view in Oracle.

The following statement returns the average salary of every department:

SELECT
AVG(salary) average_salary
FROM
employees
GROUP BY department_id;
Code language: SQL (Structured Query Language) (sql)

You can use this query as a subquery in the FROM clause to calculate the average
of average salary of departments as follows:

SELECT
ROUND(AVG(average_salary), 0)
FROM
(SELECT
AVG(salary) average_salary
FROM
employees
GROUP BY department_id) department_salary;
Code language: SQL (Structured Query Language) (sql)

SQL Subquery in the SELECT clause

A subquery can be used anywhere an expression can be used in


the SELECT clause. The following example finds the salaries of all employees,
their average salary, and the difference between the salary of each employee and
the average salary.

SELECT
employee_id,
first_name,
last_name,
salary,
(SELECT
ROUND(AVG(salary), 0)
FROM
employees) average_salary,
salary - (SELECT
ROUND(AVG(salary), 0)
FROM
employees) difference
FROM
employees
ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

Introduction to SQL correlated subquery

Let’s start with an example.

See the following employees table in the sample database:


The following query finds employees whose salary is greater than the average
salary of all employees:

SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary > (SELECT
AVG(salary)
FROM
employees);
Code language: SQL (Structured Query Language) (sql)
In this example, the subquery is used in the WHERE clause. There are some points
that you can see from this query:

First, you can execute the subquery that returns the average salary of all employees
independently.

SELECT
AVG(salary)
FROM
employees;
Code language: SQL (Structured Query Language) (sql)

Second, the database system needs to evaluate the subquery only once.

Third, the outer query makes use of the result returned from the subquery. The
outer query depends on the subquery for its value. However, the subquery does not
depend on the outer query. Sometimes, we call this subquery is a plain subquery.

Unlike a plain subquery, a correlated subquery is a subquery that uses the values
from the outer query. Also, a correlated subquery may be evaluated once for each
row selected by the outer query. Because of this, a query that uses a correlated
subquery may be slow.

A correlated subquery is also known as a repeating subquery or a synchronized


subquery.

SQL correlated subquery examples


Let’s see few more examples of the correlated subqueries to understand them
better.

SQL correlated subquery in the WHERE clause example

The following query finds all employees whose salary is higher than the average
salary of the employees in their departments:

SELECT
employee_id,
first_name,
last_name,
salary,
department_id
FROM
employees e
WHERE
salary > (SELECT
AVG(salary)
FROM
employees
WHERE
department_id = e.department_id)
ORDER BY
department_id ,
first_name ,
last_name;
Code language: SQL (Structured Query Language) (sql)

Here is the output:


In this example, the outer query is:

SELECT
employee_id,
first_name,
last_name,
salary,
department_id
FROM
employees e
WHERE
salary >
...
Code language: SQL (Structured Query Language) (sql)

and the correlated subquery is:

SELECT
AVG( list_price )
FROM
products
WHERE
category_id = p.category_id
Code language: SQL (Structured Query Language) (sql)

For each employee, the database system has to execute the correlated subquery
once to calculate the average salary of the employees in the department of the
current employee.
SQL correlated subquery in the SELECT clause example

The following query returns the employees and the average salary of all employees
in their departments:

SELECT
employee_id,
first_name,
last_name,
department_name,
salary,
(SELECT
ROUND(AVG(salary),0)
FROM
employees
WHERE
department_id = e.department_id) avg_salary_in_department
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
ORDER BY
department_name,
first_name,
last_name;
Code language: SQL (Structured Query Language) (sql)

The output is:


For each employee, the database system has to execute the correlated subquery
once to calculate the average salary by the employee’s department.

SQL correlated subquery with EXISTS operator example

We often use a correlated subquery with the EXISTS operator. For example, the
following query returns all employees who have no dependents:

SELECT
employee_id,
first_name,
last_name
FROM
employees e
WHERE
NOT EXISTS( SELECT
*
FROM
dependents d
WHERE
d.employee_id = e.employee_id)
ORDER BY first_name ,
last_name;
Code language: SQL (Structured Query Language) (sql)

The following picture shows the output:


SQL INNER JOIN

Introduction to the SQL INNER JOIN clause

So far, you have learned how to use the SELECT statement to query data from a
single table. However, the SELECT statement is not limited to query data from a
single table. The SELECT statement can link multiple tables together.

The process of linking tables is called joining. SQL provides many kinds of joins
such as inner join, left join, right join, full outer join, etc. This tutorial focuses on
the inner join.

Suppose, you have two tables: A and B.

Table A has four rows: (1,2,3,4) and table B has four rows: (3,4,5,6)

When table A joins with table B using the inner join, you have the result set (3,4)
that is the intersection of table A and table B.

See the following picture.


For each row in table A, the inner join clause finds the matching rows in table B. If
a row is matched, it is included in the final result set.

Suppose the columns in the A and B tables are a and b. The following statement
illustrates the inner join clause:

SELECT a
FROM A
INNER JOIN B ON b = a;
Code language: SQL (Structured Query Language) (sql)

The INNER JOIN clause appears after the FROM clause. The condition to match
between table A and table B is specified after the ON keyword. This condition is
called join condition i.e., B.n = A.n

The INNER JOIN clause can join three or more tables as long as they have
relationships, typically foreign key relationships.

For example, the following statement illustrates how to join 3 tables: A, B, and C:

SELECT
A.n
FROM A
INNER JOIN B ON B.n = A.n
INNER JOIN C ON C.n = A.n;
Code language: SQL (Structured Query Language) (sql)

SQL INNER JOIN examples

Let’s take some practical examples of using the INNER JOIN clause.

1) Using SQL INNER JOIN to join two tables example

We will use the employees and departments tables from the sample database to
demonstrate how the INNER JOIN clause works.
Each employee belongs to one and only one department while each department can
have more than one employee. The relationship between
the departments and employees is one-to-many.

The department_id column in the employees table is the foreign key column that
links the employees to the departments table.

To get the information of the department id 1,2, and 3, you use the following
statement.

SELECT
department_id,
department_name
FROM
departments
WHERE
department_id IN (1, 2, 3);
Code language: SQL (Structured Query Language) (sql)

Notice that we used the IN operator in the WHERE clause to get rows with
department_id 1, 2 and 3.
To get the information of employees who work in the department id 1, 2 and 3, you
use the following query:

SELECT
first_name,
last_name,
department_id
FROM
employees
WHERE
department_id IN (1, 2, 3)
ORDER BY
department_id;
Code language: SQL (Structured Query Language) (sql)

To combine data from these two tables, you use an inner join clause as the
following query:

SELECT
first_name,
last_name,
employees.department_id,
departments.department_id,
department_name
FROM
employees
INNER JOIN
departments ON departments.department_id = employees.department_id
WHERE
employees.department_id IN (1 , 2, 3);
Code language: SQL (Structured Query Language) (sql)

For each row in the employees table, the statement checks if the value of
the department_id column equals the value of the department_id column in
the departments table. If the condition

If the condition employees.department_id = departments.department_id is


satisfied, the combined row that includes data from rows in
both employees and departments tables are included in the result set.

Notice that both employees and departments tables have the same column
name department_id, therefore we had to qualify the department_id column using
the syntax table_name.column_name.

SQL INNER JOIN 3 tables example

Each employee holds one job while a job may be held by many employees. The
relationship between the jobs table and the employees table is one-to-many.

The following database diagram illustrates the relationships


between employees, departments and jobs tables:
The following query uses the inner join clauses to join 3 tables: employees,
departments, and jobs to get the first name, last name, job title, and department
name of employees who work in department id 1, 2, and 3.

SELECT
first_name,
last_name,
job_title,
department_name
FROM
employees e
INNER JOIN departments d ON d.department_id = e.department_id
INNER JOIN jobs j ON j.job_id = e.job_id
WHERE
e.department_id IN (1, 2, 3);
Code language: SQL (Structured Query Language) (sql)
SQL LEFT JOIN

Introduction to SQL LEFT JOIN clause

In the previous tutorial, you learned about the inner join that returns rows if there
is, at least, one row in both tables that matches the join condition. The inner join
clause eliminates the rows that do not match with a row of the other table.

The left join, however, returns all rows from the left table whether or not there is a
matching row in the right table.

Suppose we have two tables A and B. The table A has four rows 1, 2, 3 and 4. The
table B also has four rows 3, 4, 5, 6.

When we join table A with table B, all the rows in table A (the left table) are
included in the result set whether there is a matching row in the table B or not.

In SQL, we use the following syntax to join table A with table B.


SELECT
A.n
FROM
A
LEFT JOIN B ON B.n = A.n;
Code language: SQL (Structured Query Language) (sql)

The LEFT JOIN clause appears after the FROM clause. The condition that follows
the ON keyword is called the join condition B.n = A.n

SQL LEFT JOIN examples

SQL LEFT JOIN two tables examples

Let’s take a look at the countries and locations tables.

Each location belongs to one and only one country while each country can have
zero or more locations. The relationship between the countries and locations tables
is one-to-many.

The country_id column in the locations table is the foreign key that links to the
country_id column in the countries table.

To query the country names of US, UK, and China, you use the following
statement.

SELECT
country_id,
country_name
FROM
countries
WHERE
country_id IN ('US', 'UK', 'CN');
Code language: SQL (Structured Query Language) (sql)

The following query retrieves the locations located in the US, UK and China:

SELECT
country_id,
street_address,
city
FROM
locations
WHERE
country_id IN ('US', 'UK', 'CN');
Code language: SQL (Structured Query Language) (sql)

Now, we use the LEFT JOIN clause to join the countries table with the locations
table as the following query:

SELECT
c.country_name,
c.country_id,
l.country_id,
l.street_address,
l.city
FROM
countries c
LEFT JOIN locations l ON l.country_id = c.country_id
WHERE
c.country_id IN ('US', 'UK', 'CN')
Code language: SQL (Structured Query Language) (sql)
The condition in the WHERE clause is applied so that the statement only retrieves
the data from the US, UK, and China rows.

Because we use the LEFT JOIN clause, all rows that satisfy the condition in the
WHERE clause of the countries table are included in the result set.

For each row in the countries table, the LEFT JOIN clause finds the matching rows
in the locations table.

If at least one matching row found, the database engine combines the data from
columns of the matching rows in both tables.

In case there is no matching row found e.g., with the country_id CN, the row in the
countries table is included in the result set and the row in the locations table is
filled with NULL values.

Because non-matching rows in the right table are filled with the NULL values, you
can apply the LEFT JOIN clause to miss-match rows between tables.

For example, to find the country that does not have any locations in the locations
table, you use the following query:

SELECT
country_name
FROM
countries c
LEFT JOIN locations l ON l.country_id = c.country_id
WHERE
l.location_id IS NULL
ORDER BY
country_name;
Code language: SQL (Structured Query Language) (sql)
SQL LEFT JOIN 3 tables example

See the following tables: regions, countries, and locations.

One region may have zero or many countries while each country is located in the
one region. The relationship between countries and regions tables is one-to-many.
The region_id column in the countries table is the link between the countries and
regions table.

The following statement demonstrates how to join 3 tables: regions, countries, and
locations:

SELECT
r.region_name,
c.country_name,
l.street_address,
l.city
FROM
regions r
LEFT JOIN countries c ON c.region_id = r.region_id
LEFT JOIN locations l ON l.country_id = c.country_id
WHERE
c.country_id IN ('US', 'UK', 'CN');
Code language: SQL (Structured Query Language) (sql)

SQL SELF JOIN

Introduction to SQL self-join

Sometimes, it is useful to join a table to itself. This type of join is known as the
self-join.

We join a table to itself to evaluate the rows with other rows in the same table. To
perform the self-join, we use either an inner join or left join clause.

Because the same table appears twice in a single query, we have to use the table
aliases. The following statement illustrates how to join a table to itself.

SELECT
column1,
column2,
column3,
...
FROM
table1 A
INNER JOIN table1 B ON B.column1 = A.column2;
Code language: SQL (Structured Query Language) (sql)
In this statement joins the table1 to itself using an INNER JOIN clause. A and B
are the table aliases of the table1. The B.column1 = A.column2 is the join
condition.

Besides the INNER JOIN clause, you can use the LEFT JOIN clause.

Let’s take few examples of using the self-join technique.

SQL self-join examples

See the following employees table.

The manager_id column specifies the manager of an employee. The following


statement joins the employees table to itself to query the information of who
reports to whom.

SELECT
e.first_name || ' ' || e.last_name AS employee,
m.first_name || ' ' || m.last_name AS manager
FROM
employees e
INNER JOIN
employees m ON m.employee_id = e.manager_id
ORDER BY manager;
Code language: SQL (Structured Query Language) (sql)
The president does not have any manager. In the employees table, the manager_id
of the row that contains the president is NULL.

Because the inner join clause only includes the rows that have matching rows in
the other table, therefore the president did not show up in the result set of the query
above.

To include the president in the result set, we use the LEFT JOIN clause instead of
the INNER JOIN clause as the following query.

SELECT
e.first_name || ' ' || e.last_name AS employee,
m.first_name || ' ' || m.last_name AS manager
FROM
employees e
LEFT JOIN
employees m ON m.employee_id = e.manager_id
ORDER BY manager;
Code language: SQL (Structured Query Language) (sql)
SQL FULL OUTER JOIN

Introduction to SQL FULL OUTER JOIN clause

In theory, a full outer join is the combination of a left join and a right join. The full
outer join includes all rows from the joined tables whether or not the other table
has the matching row.

If the rows in the joined tables do not match, the result set of the full outer join
contains NULL values for every column of the table that lacks a matching row. For
the matching rows, a single row that has the columns populated from the joined
table is included in the result set.

The following statement illustrates the syntax of the full outer join of two tables:

SELECT column_list
FROM A
FULL OUTER JOIN B ON B.n = A.n;
Code language: SQL (Structured Query Language) (sql)

Note that the OUTER keyword is optional.

The following Venn diagram illustrates the full outer join of two tables.
SQL FULL OUTER JOIN examples

Let’s take an example of using the FULL OUTER JOIN clause to see how it
works.

First, create two new tables: baskets and fruits for the demonstration. Each basket
stores zero or more fruits and each fruit can be stored in zero or one basket.

CREATE TABLE fruits (


fruit_id INTEGER PRIMARY KEY,
fruit_name VARCHAR (255) NOT NULL,
basket_id INTEGER
);
Code language: SQL (Structured Query Language) (sql)
CREATE TABLE baskets (
basket_id INTEGER PRIMARY KEY,
basket_name VARCHAR (255) NOT NULL
);
Code language: SQL (Structured Query Language) (sql)

Second, insert some sample data into the baskets and fruits tables.

INSERT INTO baskets (basket_id, basket_name)


VALUES
(1, 'A'),
(2, 'B'),
(3, 'C');
Code language: SQL (Structured Query Language) (sql)
INSERT INTO fruits (
fruit_id,
fruit_name,
basket_id
)
VALUES
(1, 'Apple', 1),
(2, 'Orange', 1),
(3, 'Banana', 2),
(4, 'Strawberry', NULL);
Code language: SQL (Structured Query Language) (sql)

Third, the following query returns each fruit that is in a basket and each basket that
has a fruit, but also returns each fruit that is not in any basket and each basket that
does not have any fruit.

SELECT
basket_name,
fruit_name
FROM
fruits
FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id;

SQL CROSS JOIN

Introduction to SQL CROSS JOIN clause

A cross join is a join operation that produces the Cartesian product of two or more
tables.

In Math, a Cartesian product is a mathematical operation that returns a product set


of multiple sets.

For example, with two sets A {x,y,z} and B {1,2,3}, the Cartesian product of A x
B is the set of all ordered pairs (x,1), (x,2), (x,3), (y,1) (y,2), (y,3), (z,1), (z,2),
(z,3).

The following picture illustrates the Cartesian product of A and B:


Similarly, in SQL, a Cartesian product of two tables A and B is a result set in
which each row in the first table (A) is paired with each row in the second table
(B). Suppose the A table has n rows and the B table has m rows, the result of the
cross join of the A and B tables have n x m rows.

The following illustrates syntax of the CROSS JOIN clause:

SELECT column_list
FROM A
CROSS JOIN B;
Code language: SQL (Structured Query Language) (sql)

The following picture illustrates the result of the cross join between the table A and
table B. In this illustration, the table A has three rows 1, 2 and 3 and the table B
also has three rows x, y and z. As the result, the Cartesian product has nine rows:
Note that unlike the INNER JOIN, LEFT JOIN, and FULL OUTER JOIN,
the CROSS JOIN clause does not have a join condition.

The following statement is equivalent to the one that uses the CROSS JOIN clause
above:

SELECT
column_list
FROM
A,
B;
Code language: SQL (Structured Query Language) (sql)

SQL CROSS JOIN example

We will create two new tables for the demonstration of the cross join:

 sales_organization table stores the sale organizations.


 sales_channel table stores the sales channels.

The following statements create the sales_organization and sales_channel tables:

CREATE TABLE sales_organization (


sales_org_id INT PRIMARY KEY,
sales_org VARCHAR (255)
);
Code language: SQL (Structured Query Language) (sql)
CREATE TABLE sales_channel (
channel_id INT PRIMARY KEY,
channel VARCHAR (255)
);
Code language: SQL (Structured Query Language) (sql)

Suppose the company has two sales organizations that are Domestic and Export,
which are in charge of sales in the domestic and international markets.

The following statement inserts two sales organizations into


the sales_organization table:

INSERT INTO sales_organization (sales_org_id, sales_org)


VALUES
(1, 'Domestic'),
(2, 'Export');
Code language: SQL (Structured Query Language) (sql)

The company can distribute goods via various channels such as wholesale, retail,
eCommerce, and TV shopping. The following statement inserts sales channels into
the sales_channel table:

INSERT INTO sales_channel (channel_id, channel)


VALUES
(1, 'Wholesale'),
(2, 'Retail'),
(3, 'eCommerce'),
(4, 'TV Shopping');
Code language: SQL (Structured Query Language) (sql)

To find the all possible sales channels that a sales organization can have, you use
the CROSS JOIN to join the sales_organization table with the sales_channel table
as follows:

SELECT
sales_org,
channel
FROM
sales_organization
CROSS JOIN sales_channel;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:


The result set includes all possible rows in
the sales_organization and sales_channel tables.

The following query is equivalent to the statement that uses the CROSS
JOIN clause above:

SELECT
sales_org,
channel
FROM
sales_organization,
sales_channel;
Code language: SQL (Structured Query Language) (sql)

SQL GROUP BY

Introduction to SQL GROUP BY clause

Grouping is one of the most important tasks that you have to deal with while
working with data. To group rows into groups, you use the GROUP BY clause.

The GROUP BY clause is an optional clause of the SELECT statement that


combines rows into groups based on matching values in specified columns. One
row is returned for each group.

You often use the GROUP BY in conjunction with an aggregate function such
as MIN, MAX, AVG, SUM, or COUNT to calculate a measure that provides the
information for each group.

The following illustrates the syntax of the GROUP BY clause.

SELECT
column1,
column2,
AGGREGATE_FUNCTION (column3)
FROM
table1
GROUP BY
column1,
column2;
Code language: SQL (Structured Query Language) (sql)
It is not mandatory to include an aggregate function in the SELECT clause.
However, if you use an aggregate function, it will calculate the summary value for
each group.

If you want to filter the rows before grouping, you add a WHERE clause.
However, to filter groups, you use the HAVING clause.

It is important to emphasize that the WHERE clause is applied before rows are
grouped whereas the HAVING clause is applied after rows are grouped. In other
words, the WHERE clause is applied to rows whereas the HAVING clause is
applied to groups.

To sort the groups, you add the ORDER BY clause after the GROUP BY clause.

The columns that appear in the GROUP BY clause are called grouping columns. If
a grouping column contains NULL values, all NULL values are summarized into a
single group because the GROUP BY clause considers NULL values are equal.

SQL GROUP BY examples

We will use the employees and departments tables in the sample database to
demonstrate how the GROUP BY clause works.

To find the headcount of each department, you group the employees by


the department_id column, and apply the COUNT function to each group as the
following query:
SELECT
department_id,
COUNT(employee_id) headcount
FROM
employees
GROUP BY
department_id;
Code language: SQL (Structured Query Language) (sql)

SQL GROUP BY with INNER JOIN example

To get the department name, you join the employees table with
the departments table as follows:

SELECT
e.department_id,
department_name,
COUNT(employee_id) headcount
FROM
employees e
INNER JOIN departments d ON d.department_id = e.department_id
GROUP BY
e.department_id;
Code language: SQL (Structured Query Language) (sql)
SQL GROUP BY with ORDER BY example

To sort the departments by headcount, you add an ORDER BY clause as the


following statement:

SELECT
e.department_id,
department_name,
COUNT(employee_id) headcount
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id
ORDER BY headcount DESC;
Code language: SQL (Structured Query Language) (sql)
Note that you can use either the headcount alias or the COUNT(employee_id) in
the ORDER BY clause.

SQL GROUP BY with HAVING example

To find the department whose headcount is greater than 5, you use


the HAVING clause like the following query:

SELECT
e.department_id,
department_name,
COUNT(employee_id) headcount
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id
HAVING headcount > 5
ORDER BY headcount DESC;
Code language: SQL (Structured Query Language) (sql)
SQL GROUP BY with MIN, MAX, and AVG example

The following query returns the minimum, maximum, and average salary of
employees in each department.

SELECT
e.department_id,
department_name,
MIN(salary) min_salary,
MAX(salary) max_salary,
ROUND(AVG(salary), 2) average_salary
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id;
Code language: SQL (Structured Query Language) (sql)

SQL GROUP BY with SUM function example

To get the total salary per department, you apply the SUM function to
the salary column and group employees by the department_id column as follows:

SELECT
e.department_id,
department_name,
SUM(salary) total_salary
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL GROUP BY multiple columns

So far, you have seen that we have grouped all employees by one column. For
example, the following clause

GROUP BY department_id
Code language: SQL (Structured Query Language) (sql)

place all rows with the same values in the department_id column in one group.

How about grouping employees by values in


both department_id and job_id columns?

GROUP BY department_id, job_id


Code language: SQL (Structured Query Language) (sql)

This clause will group all employees with the same values in
both department_id and job_id columns in one group.
The following statement groups rows with the same values in
both department_id and job_id columns in the same group then return the rows for
each of these groups.

SELECT
e.department_id,
department_name,
e.job_id,
job_title,
COUNT(employee_id)
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
INNER JOIN
jobs j ON j.job_id = e.job_id
GROUP BY e.department_id , e.job_id;
Code language: SQL (Structured Query Language) (sql)

Try It

Department 2, 3 and 5 appear more than one.

This is because these departments have employees who hold different jobs. For
example, in the shipping department, there are 2 employees holding the shipping
clerk job, 1 employee holding the stock clerk job, and 4 employees holding the
stock manager job.
SQL GROUP BY and DISTINCT

If you use the GROUP BY clause without an aggregate function, the GROUP
BY clause behaves like the DISTINCT operator.

The following gets the phone numbers of employees and also groups rows by the
phone numbers.

SELECT
phone_number
FROM
employees
GROUP BY
phone_number;
Code language: SQL (Structured Query Language) (sql)

Try It

Notice that the phone numbers are sorted.

The following statement also retrieves the phone numbers but instead of using
the GROUP BY clause, it uses the DISTINCT operator.

SELECT DISTINCT
phone_number
FROM
employees;

References:

1. https://www.sqltutorial.org/

You might also like