SQL Keyword: Inner Join
SQL Keyword: Inner Join
SQL Keyword: Inner Join
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
2 Chang 1 1 24 - 12 oz bottles
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Try it yourself »
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
Try it yourself »
Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
Try it yourself »
BETWEEN Operator with Text Value Example
The following SQL statement selects all products with a ProductName beginning with any of
the letter BETWEEN 'C' and 'M':
Example
SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';
Try it yourself »
Example
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'C' AND 'M';
Try it yourself »
Sample Table
Below is a selection from the "Orders" table:
10248 90 5 7/4/1996 3
10249 81 6 7/5/1996 1
10250 34 4 7/8/1996 2
10251 84 3 7/9/1996 1
10252 76 4 7/10/1996 2
Example
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
Try it yourself »
Notice that the BETWEEN operator can produce different result in different databases!
In some databases, BETWEEN selects fields that are between and excluding the test values.
In other databases, BETWEEN selects fields that are between and including the test values.
And in other databases, BETWEEN selects fields between the test values, including the first test value and
excluding the last test value.
3º SQL Joins
« Previous
Next Chapter »
SQL joins are used to combine rows from two or more tables.
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a common
field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN
return all rows from multiple tables where the join condition is met.
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Notice that the "CustomerID" column in the "Orders" table refers to the customer in the
"Customers" table. The relationship between the two tables above is the "CustomerID"
column.
Then, if we run the following SQL statement (that contains an INNER JOIN):
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Try it yourself »
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right
table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the
left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
4º SQL Aliases
« Previous
Next Chapter »
SQL Aliases
SQL aliases are used to give a database table, or a column in a table, a temporary name.
SELECT column_name(s)
FROM table_name AS alias_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP
10354 58 8 1996-11-14 3
10355 4 6 1996-11-15 1
10356 86 6 1996-11-18 2
Example
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
Try it yourself »
In the following SQL statement we combine four columns (Address, City, PostalCode, and
Country) and create an alias named "Address":
Example
SELECT CustomerName, Address+', '+City+', '+PostalCode+', '+Country AS
Address
FROM Customers;
Try it yourself »
Note: To get the SQL statement above to work in MySQL use the following:
Example
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName="Around the Horn" AND c.CustomerID=o.CustomerID;
Try it yourself »
Example
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName="Around the Horn" AND
Customers.CustomerID=Orders.CustomerID;
Try it yourself »
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Customers" table:
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Example
SELECT * FROM Customers
WHERE City LIKE 's%';
Try it yourself »
Tip: The "%" sign is used to define wildcards (missing letters) both before and after the
pattern. You will learn more about wildcards in the next chapter.
The following SQL statement selects all customers with a City ending with the letter "s":
Example
SELECT * FROM Customers
WHERE City LIKE '%s';
Try it yourself »
The following SQL statement selects all customers with a Country containing the pattern
"land":
Example
SELECT * FROM Customers
WHERE Country LIKE '%land%';
Try it yourself »
Using the NOT keyword allows you to select records that does NOT match the pattern.
The following SQL statement selects all customers with a Country NOT containing the pattern
"land":
Example
SELECT * FROM Customers
WHERE Country NOT LIKE '%land%';
7º SQL Wildcards
« Previous
Next Chapter »
A wildcard character can be used to substitute for any other character(s) in a string.
Wildcard Description
Demo Database
In this tutorial we will use the well-known Northwind sample database.
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP
Try it yourself »
The following SQL statement selects all customers with a City containing the pattern "es":
Example
SELECT * FROM Customers
WHERE City LIKE '%es%';
Try it yourself »
Example
SELECT * FROM Customers
WHERE City LIKE '_erlin';
Try it yourself »
The following SQL statement selects all customers with a City starting with "L", followed by
any character, followed by "n", followed by any character, followed by "on":
Example
SELECT * FROM Customers
WHERE City LIKE 'L_n_on';
Try it yourself »
Example
SELECT * FROM Customers
WHERE City LIKE '[bsp]%';
Try it yourself »
The following SQL statement selects all customers with a City starting with "a", "b", or "c":
Example
SELECT * FROM Customers
WHERE City LIKE '[a-c]%';
Try it yourself »
The following SQL statement selects all customers with a City NOT starting with "b", "s", or
"p":
Example
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
Try it yourself »
8º SQL IN Operator
« Previous
Next Chapter »
The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
Demo Database
In this tutorial we will use the well-known Northwind sample database.
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP
IN Operator Example
The following SQL statement selects all customers with a City of "Paris" or "London":
Example
SELECT * FROM Customers
WHERE City IN ('Paris','London');
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Try it yourself »
Note: The LEFT JOIN keyword returns all the rows from the left table (Customers), even if
there are no matches in the right table (Orders).
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
Example
SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;
Try it yourself »
Note: The RIGHT JOIN keyword returns all the rows from the right table (Employees), even
if there are no matches in the left table (Orders).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
CustomerName OrderID
Alfreds Futterkiste
10382
10351
Note: The FULL OUTER JOIN keyword returns all the rows from the left table (Customers),
and all the rows from the right table (Orders). If there are rows in "Customers" that do not
have matches in "Orders", or if there are rows in "Orders" that do not have matches in
"Customers", those rows will be listed as well.
The SQL UNION operator combines the result of two or more SELECT statements.
The SQL UNION Operator
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of
columns. The columns must also have similar data types. Also, the columns in each SELECT
statement must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow duplicate values,
use the ALL keyword with UNION.
PS: The column names in the result-set of a UNION are usually equal to the column names
in the first SELECT statement in the UNION.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117
3 Grandma Kelly's Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104
SQL UNION Example
The following SQL statement selects all the different cities (only distinct values) from the
"Customers" and the "Suppliers" tables:
Example
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Try it yourself »
Note: UNION cannot be used to list ALL cities from the two tables. If several customers and
suppliers share the same city, each city will only be listed once. UNION selects only distinct
values. Use UNION ALL to also select duplicate values!
Example
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
With SQL, you can copy information from one table into another.
The INSERT INTO SELECT statement copies data from one table and inserts it into an
existing table.
We can copy all columns from one table to another, existing table:
INSERT INTO table2
SELECT * FROM table1;
Or we can copy only the columns we want to into another, existing table:
Demo Database
In this tutorial we will use the well-known Northwind sample database.
2 New Orleans Cajun Shelley Burke P.O. Box New 70117 USA (10
Delights 78934 Orleans 482
3 Grandma Kelly's Regina Murphy 707 Oxford Ann Arbor 48104 USA (31
Homestead Rd. 573
Example
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;
Try it yourself »
Tables are organized into rows and columns; and each table must have a name.
The data_type parameter specifies what type of data the column can hold (e.g. varchar,
integer, decimal, date, etc.).
The size parameter specifies the maximum length of the column of the table.
Tip: For an overview of the data types available in MS Access, MySQL, and SQL Server, go to
our complete Data Types Reference.
Example
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Try it yourself »
The LastName, FirstName, Address, and City columns are of type varchar and will hold
characters, and the maximum length for these fields is 255 characters.
Tip: The empty table can be filled with data with the INSERT INTO statement.