You are on page 1of 88

SQL SELECT Statement

 The SELECT statement retrieves data from a database.

 The data is returned in a table-like structure called a result-set.

 SELECT is the most frequently used action on a database.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL SELECT syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name

To select all columns use *

1. SELECT *
2. FROM table-name

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
SQL SELECT Examples

Problem: List all customers

1. SELECT *
2. FROM Customer

Results: 91 records

Id FirstName LastName City Country Phone

1 Maria Anders Berlin Germany 030-0074321

2 Ana Trujillo México D.F. Mexico (5) 555-4729

3 Antonio Moreno México D.F. Mexico (5) 555-3932

4 Thomas Hardy London UK (171) 555-7788

5 Christina Berglund Luleå Sweden 0921-12 34 65

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List the first name, last name, and city of all customers

1. SELECT FirstName, LastName, City


2. FROM Customer

Results: 91 records

FirstName LastName City

Maria Anders Berlin

Ana Trujillo México D.F.

Antonio Moreno México D.F.


Thomas Hardy London

Christina Berglund Luleå

SQL WHERE Clause

 To limit the number of rows use the WHERE clause.

 The WHERE clause filters for rows that meet certain criteria.

 WHERE is followed by a condition that returns either true or false.

 WHERE is used with SELECT, UPDATE, and DELETE.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL WHERE syntax


A WHERE clause with a SELECT statement:

1. SELECT column-names
2. FROM table-name
3. WHERE condition

A WHERE clause with an UPDATE statement:

1. UPDATE table-name
2. SET column-name = value
3. WHERE condition
A WHERE clause with a DELETE statement:

1. DELETE table-name
2. WHERE condition

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL WHERE Clause Examples

Problem: List the customers in Sweden

1. SELECT Id, FirstName, LastName, City, Country, Phone


2. FROM Customer
3. WHERE Country = 'Sweden'

Results: 2 records

Id FirstName LastName City Country Phone

5 Christina Berglund Luleå Sweden 0921-12 34 65

24 Maria Larsson Bräcke Sweden 0695-34 67 21

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

Problem: Update the city to Sydney for supplier Pavlova, Ltd.

1. UPDATE Supplier
2. SET City = 'Sydney'
3. WHERE Name = 'Pavlova, Ltd.'

Results: 1 record updated.


PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Delete all products with unit price higher than $50.

1. DELETE FROM Product


2. WHERE UnitPrice > 50

Results: 7 records deleted.

Note: Referential integrity may prevent this deletion.


A better approach may be to discontinue the product, that is, set IsDiscontinued to true.

SQL INSERT INTO Statement

 The INSERT INTO statement is used to add new data to a database.

 The INSERT INTO statement adds a new record to a table.

 INSERT INTO can contain values for some or all of its columns.

 INSERT INTO can be combined with a SELECT to insert records.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL INSERT INTO syntax


The general syntax is:

1. INSERT INTO table-name (column-names)


2. VALUES (values)

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL INSERT INTO Examples

Problem: Add a record for a new customer

1. INSERT INTO Customer (FirstName, LastName, City, Country, Phone)


2. VALUES ('Craig', 'Smith', 'New York', 'USA', 1-01-993 2800)

Results: 1 new record inserted

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: Add a new customer named Anita Coats to the database

1. INSERT INTO Customer (FirstName, LastName)


2. VALUES ('Anita', 'Coats')

Results: 1 new record inserted

The SQL INSERT combined with a SELECT


The general syntax is:

1. INSERT INTO table-name (column-names)


2. SELECT column-names
3. FROM table-name
4. WHERE condition

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL INSERT INTO with SELECT Example

Problem: The Bigfoot Brewery supplier is also a customer.


Add a customer record with values from the supplier table.

1. INSERT INTO Customer (FirstName, LastName, City, Country, Phone)


2. SELECT LEFT(ContactName, CHARINDEX(' ',ContactName) - 1),
3. SUBSTRING(ContactName, CHARINDEX(' ',ContactName) + 1, 100),
4. City, Country, Phone
5. FROM Supplier
6. WHERE CompanyName = 'Bigfoot Breweries'

Note: ContactName is parsed into FirstName and LastName.


Parsing takes place with built-in functions: LEFT, SUBSTRING, and CHARINDEX.

Results: 1 new record inserted

SQL UPDATE Statement

 The UPDATE statement updates data values in a database.

 UPDATE can update one or more records in a table.

 Use the WHERE clause to UPDATE only specific records.

The definitive guide


for data professionals
See 2 min video

Previous

Next

The SQL UPDATE syntax


The general syntax is:

1. UPDATE table-name
2. SET column-name = value, column-name = value, ...

To limit the number of records to UPDATE append a WHERE clause:

1. UPDATE table-name
2. SET column-name = value, column-name = value, ...
3. WHERE condition

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL UPDATE Examples

Problem: discontinue all products in the database

1. UPDATE Product
2. SET IsDiscontinued = 1
Note: the value 1 denotes true.

Results: 77 records updated.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Discontinue products over $50.

1. UPDATE Product
2. SET IsDiscontinued = 1
3. WHERE UnitPrice > 50

Note: the value 1 denotes true.

Results: 7 records updated.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Discontinue product with Id = 46.

1. UPDATE Product
2. SET IsDiscontinued = 1
3. WHERE Id = 46

This is a more common scenario in which a single record is updated.


Note: the value 1 denotes true.

Results: 1 record updated.

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
Problem: Supplier Norske Meierier (Id = 15) has moved: update their city, phone and fax.

1. UPDATE Supplier
2. SET City = 'Oslo', Phone = '(0)1-953530', Fax = '(0)1-953555'
3. WHERE Id = 15

This is a common scenario in which a single record is updated.

Results: 1 record updated.

SQL DELETE Statement

 DELETE permanently removes records from a table.

 DELETE can delete one or more records in a table.

 Use the WHERE clause to DELETE only specific records.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL DELETE syntax


The general syntax is:

1. DELETE table-name

To delete specific records append a WHERE clause:

1. DELETE table-name
2. WHERE condition
PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL DELETE Examples

Problem: Delete all products.

1. DELETE Product

Results: 77 records deleted.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Delete products over $50.

1. DELETE Product
2. WHERE UnitPrice > 50

Results: 7 records deleted.

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: Delete customer with Id = 21.

1. DELETE Customer
2. WHERE Id = 21

This is a more common scenario in which a single record is deleted.

Results: 1 record deleted.

QL ORDER BY Clause

 SELECT returns records in no particular order.

 To ensure a specific order use the ORDER BY clause.

 ORDER BY allows sorting by one or more columns.

 Records can be returned in ascending or descending order.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL ORDER BY syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE condition
4. ORDER BY column-names

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL ORDER BY Examples

Problem: List all suppliers in alphabetical order

1. SELECT CompanyName, ContactName, City, Country


2. FROM Supplier
3. ORDER BY CompanyName

The default sort order is ascending, that is, low-high or a-z.

Results: 29 records

Id CompanyName ContactName City Country

18 Aux joyeux ecclésiastiques Guylène Nodier Paris France

16 Bigfoot Breweries Cheryl Saylor Bend USA

5 Cooperativa de Quesos 'Las Antonio del Valle Oviedo Spain


Cabras' Saavedra

27 Escargots Nouveaux Marie Delamare Montceau France

1 Exotic Liquids Charlotte Cooper London UK

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List all suppliers in reverse alphabetical order

1. SELECT CompanyName, ContactName, City, Country


2. FROM Supplier
3. ORDER BY CompanyName DESC

The keyword DESC denotes descending, i.e., reverse order.

Results: 29 records
Id CompanyName ContactName City Country

22 Zaanse Snoepfabriek Dirk Luchte Zaandam Netherlands

4 Tokyo Traders Yoshi Nagase Tokyo Japan

17 Svensk Sjöföda AB Michael Björn Stockholm Sweden

8 Specialty Biscuits, Ltd. Peter Wilson Manchester UK

10 Refrescos Americanas LTDA Carlos Diaz Sao Paulo Brazil

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List all customers ordered by country, then by city within each country
Ordering by one or more columns is possible.

1. SELECT FirstName, LastName, City, Country


2. FROM Customer
3. ORDER BY Country, City

Results: 91 records

Id FirstName LastName City Country

12 Patricio Simpson Buenos Aires Argentina

54 Yvonne Moncada Buenos Aires Argentina

64 Sergio Gutiérrez Buenos Aires Argentina

20 Roland Mendel Graz Austria

59 Georg Pipps Salzburg Austria

50 Catherine Dewey Bruxelles Belgium

76 Pascale Cartrain Charleroi Belgium


SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

Problem: List all suppliers in the USA, Japan, and Germany, ordered by city,
then by company name in reverse order

1. SELECT Id, CompanyName, City, Country


2. FROM Supplier
3. WHERE Country IN ('USA', 'Japan', 'Germany')
4. ORDER BY Country ASC, CompanyName DESC

This shows that you can order by more than one column.
ASC denotes ascending, but is optional as it is the default sort order.

Results: 9 records

Id CompanyName City Country

12 Plutzer Lebensmittelgroßmärkte AG Frankfurt Germany

13 Nord-Ost-Fisch Handelsgesellschaft mbH Cuxhaven Germany

11 Heli Süßwaren GmbH & Co. KG Berlin Germany

4 Tokyo Traders Tokyo Japan

6 Mayumi's Osaka Japan

2 New Orleans Cajun Delights New Orleans USA

19 New England Seafood Cannery Boston USA

3 Grandma Kelly's Homestead Ann Arbor USA

16 Bigfoot Breweries Bend USA

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: Show all orders, sorted by total amount, the largest first, within each year

1. SELECT Id, OrderDate, CustomerId, TotalAmount


2. FROM [Order]
3. ORDER BY YEAR(OrderDate) ASC, TotalAmount DESC

Note: DESC means descending, but is optional as it is the default sort order.
[Order] must be bracketed because it also is a keywork in SQL.
Results: 830 records.

Id OrderDate CustomerId TotalAmount

125 2012-12-04 00:00:00.000 62 12281.20

106 2012-11-13 00:00:00.000 59 10741.60

113 2012-11-22 00:00:00.000 7 7390.20

144 2012-12-23 00:00:00.000 17 86.40

24 2012-08-01 00:00:00.000 75 48.00

177 2013-01-23 00:00:00.000 51 11493.20

170 2013-01-16 00:00:00.000 73 11283.20

560 2013-12-31 00:00:00.000 27 18.40

535 2013-12-17 00:00:00.000 12 12.50

618 2014-02-02 00:00:00.000 63 17250.00

783 2014-04-17 00:00:00.000 71 16321.90

Notice the year breakpoints: 2012 - 2013 and 2013 - 2014. Each year starts with the highest TotalAmounts.
This shows that other data types, such as numbers, dates, and bits can also be sorted.
Note: YEAR is a built-in function which returns the year from a date.

SQL SELECT TOP Statement

 The SELECT TOP statement returns a specified number of records.

 SELECT TOP is useful when working with very large datasets.

 Non SQL Server databases use keywords like LIMIT, OFFSET, and ROWNUM.
The definitive guide
for data professionals

See 2 min video

Previous

Next

The SQL SELECT TOP syntax


The general syntax is:

1. SELECT TOP n column-names


2. FROM table-name

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL SELECT TOP

Problem: List top 10 most expensive products

1. SELECT TOP 10 Id, ProductName, UnitPrice, Package


2. FROM Product
3. ORDER BY UnitPrice DESC

Results: 10 records.
Id ProductName UnitPrice Package

38 Côte de Blaye 263.50 12 - 75 cl bottles

29 Thüringer Rostbratwurst 123.79 50 bags x 30 sausgs.

9 Mishi Kobe Niku 97.00 18 - 500 g pkgs.

20 Sir Rodney's Marmalade 81.00 30 gift boxes

18 Carnarvon Tigers 62.50 16 kg pkg.

59 Raclette Courdavault 55.00 5 kg pkg.

51 Manjimup Dried Apples 53.00 50 - 300 g pkgs.

62 Tarte au sucre 49.30 48 pies

43 Ipoh Coffee 46.00 16 - 500 g tins

28 Rössle Sauerkraut 45.60 25 - 825 g cans

SQL OFFSET-FETCH Clause

 OFFSET excludes the first set of records.

 OFFSET can only be used with an ORDER BY clause.

 OFFSET with FETCH NEXT returns a defined window of records.

 OFFSET with FETCH NEXT is great for building pagination support.

The definitive guide


for data professionals

See 2 min video

Previous
Next

The SQL ORDER BY OFFSET syntax


The general syntax to exclude first n records is:

1. SELECT column-names
2. FROM table-name
3. ORDER BY column-names
4. OFFSET n ROWS

To exclude first n records and return only the next m records:

1. SELECT column-names
2. FROM table-name
3. ORDER BY column-names
4. OFFSET n ROWS
5. FETCH NEXT m ROWS ONLY

This will return only record (n + 1) to (n + 1 + m). See example below.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL OFFSET-FETCH Examples

Problem: Get all but the 10 most expensive products sorted by price

1. SELECT Id, ProductName, UnitPrice, Package


2. FROM Product
3. ORDER BY UnitPrice DESC
4. OFFSET 10 ROWS

Results: 68 records.

Id ProductName UnitPrice Package

27 Schoggi Schokolade 43.90 100 - 100 g pieces

63 Vegie-spread 43.90 15 - 625 g jars

8 Northwoods Cranberry Sauce 40.00 12 - 12 oz jars

17 Alice Mutton 39.00 20 - 1 kg tins


12 Queso Manchego La Pastora 38.00 10 - 500 g pkgs.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Get the 10th to 15th most expensive products sorted by price

1. SELECT Id, ProductName, UnitPrice, Package


2. FROM Product
3. ORDER BY UnitPrice DESC
4. OFFSET 10 ROWS
5. FETCH NEXT 5 ROWS ONLY

Results: 5 records

Id ProductName UnitPrice Package

27 Schoggi Schokolade 43.90 100 - 100 g pieces

63 Vegie-spread 43.90 15 - 625 g jars

8 Northwoods Cranberry Sauce 40.00 12 - 12 oz jars

17 Alice Mutton 39.00 20 - 1 kg tins

12 Queso Manchego La Pastora 38.00 10 - 500 g pkgs.

SQL SELECT DISTINCT Statement

 SELECT DISTINCT returns only distinct (different) values.

 SELECT DISTINCT eliminates duplicate records from the results.

 DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.

 DISTINCT operates on a single column. DISTINCT for multiple columns is not supported.

The definitive guide


for data professionals
See 2 min video

Previous

Next

The SQL SELECT DISTINCT syntax


The general syntax is:

1. SELECT DISTINCT column-name


2. FROM table-name

Can be used with COUNT and other aggregates

1. SELECT COUNT (DISTINCT column-name)


2. FROM table-name

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

SQL SELECT Examples

Problem: List all supplier countries in alphabetical order.

1. SELECT DISTINCT Country


2. FROM Supplier
3. ORDER BY COUNTRY

Results: 16 rows

Country

Australia

Brazil

Canada

Denmark

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

Problem: List the number of supplier countries

1. SELECT COUNT (DISTINCT Country)


2. FROM Supplier

Results:

Count

16

QL SELECT MIN, MAX Statement

 SELECT MIN returns the minimum value for a column.

 SELECT MAX returns the maximum value for a column.

The definitive guide


for data professionals
See 2 min video

Previous

Next

The SQL SELECT MIN and MAX syntax


The general MIN syntax is:

1. SELECT MIN(column-name)
2. FROM table-name

The general MAX syntax is:

1. SELECT MAX(column-name)
2. FROM table-name

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Find the cheapest product

1. SELECT MIN(UnitPrice)
2. FROM Product

Results:
UnitPrice

2.50

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

SQL SELECT MAX and MIN Examples

Problem: Find the largest order placed in 2014

1. SELECT MAX(TotalAmount)
2. FROM [Order]
3. WHERE YEAR(OrderDate) = 2014

Results:

TotalAmount

17250.00

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: Find the last order date in 2013

1. SELECT MAX(OrderDate)
2. FROM [Order]
3. WHERE YEAR(OrderDate) = 2013

MIN and MAX can also be used with numeric and date types.
Results:

OrderDate

2013-12-31
00:00:00.000

SQL SELECT COUNT, SUM, AVG

 SELECT COUNT returns a count of the number of data values.

 SELECT SUM returns the sum of the data values.

 SELECT AVG returns the average of the data values.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL SELECT COUNT, SUM, and AVG syntax


The general COUNT syntax is:

1. SELECT COUNT(column-name)
2. FROM table-name

The general SUM syntax is:

1. SELECT SUM(column-name)
2. FROM table-name

The general AVG syntax is:

1. SELECT AVG(column-name)
2. FROM table-name
CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL SELECT COUNT, SUM, and AVG Examples

Problem: Find the number of customers

1. SELECT COUNT(Id)
2. FROM Customer

Results:

Count

91

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: Compute the total amount sold in 2013

1. SELECT SUM(TotalAmount)
2. FROM [Order]
3. WHERE YEAR(OrderDate) = 2013

Results:

Sum

658388.75

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: Compute the average size of all orders

1. SELECT AVG(TotalAmount)
2. FROM [Order]

Results:

Average

1631.877819

SQL WHERE AND, OR, NOT Clause

 WHERE conditions can be combined with AND, OR, and NOT.

 A WHERE clause with AND requires that two conditions are true.

 A WHERE clause with OR requires that one of two conditions is true.

 A WHERE clause with NOT negates the specified condition.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The WHERE with AND, OR, NOT syntax


A WHERE clause with AND:
1. SELECT column-names
2. FROM table-name
3. WHERE condition1 AND condition2

A WHERE clause with OR:

1. UPDATE table-name
2. SET column-name = value
3. WHERE condition1 OR condition2

A WHERE clause with NOT:

1. DELETE table-name
2. WHERE NOT condition

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL WHERE with AND, OR, and NOT Examples

Problem: Get customer named Thomas Hardy

1. SELECT Id, FirstName, LastName, City, Country


2. FROM Customer
3. WHERE FirstName = 'Thomas' AND LastName = 'Hardy'

Results: 1 record.

Id FirstName LastName City Country

4 Thomas Hardy London UK

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List all customers from Spain or France


1. SELECT Id, FirstName, LastName, City, Country
2. FROM Customer
3. WHERE Country = 'Spain' OR Country = 'France'

Results: 16 records.

Id FirstName LastName City Country

7 Frédérique Citeaux Strasbourg France

8 Martín Sommer Madrid Spain

9 Laurence Lebihan Marseille France

18 Janine Labrune Nantes France

22 Diego Roel Madrid Spain

23 Martine Rancé Lille France

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List all customers that are not from the USA

1. SELECT Id, FirstName, LastName, City, Country


2. FROM Customer
3. WHERE NOT Country = 'USA'

Results: 78 records.

Id FirstName LastName City Country

1 Maria Anders Berlin Germany

2 Ana Trujillo México D.F. Mexico

3 Antonio Moreno México D.F. Mexico

4 Thomas Hardy London UK


5 Christina Berglund Luleå Sweden

6 Hanna Moos Mannheim Germany

7 Frédérique Citeaux Strasbourg France

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: List all orders that not between $50 and $15000

1. SELECT Id, OrderDate, CustomerId, TotalAmount


2. FROM [Order]
3. WHERE NOT (TotalAmount >= 50 AND TotalAmount <= 15000)
4. ORDER BY TotalAmount DESC

Results: 16 records.

Id OrderDate CustomerId TotalAmount

618 2/2/2014 12:00:00 AM 63 17250.00

783 4/17/2014 12:00:00 AM 71 16321.90

734 3/27/2014 12:00:00 AM 34 15810.00

175 1/22/2013 12:00:00 AM 27 49.80

24 8/1/2012 12:00:00 AM 75 48.00

SQL WHERE BETWEEN Clause

 WHERE BETWEEN returns values that fall within a given range.

 WHERE BETWEEN is a shorthand for >= AND <=.

 BETWEEN operator is inclusive: begin and end values are included.

The definitive guide


for data professionals
See 2 min video

Previous

Next

The SQL WHERE BETWEEN syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE column-name BETWEEN value1 AND value2

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL WHERE BETWEEN Examples

Problem: List all products between $10 and $20

1. SELECT Id, ProductName, UnitPrice


2. FROM Product
3. WHERE UnitPrice BETWEEN 10 AND 20
4. ORDER BY UnitPrice

Results: 29 records.
Id ProductName UnitPrice

3 Aniseed Syrup 10.00

21 Sir Rodney's Scones 10.00

74 Longlife Tofu 10.00

46 Spegesild 12.00

31 Gorgonzola Telino 12.50

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: List all products not between $10 and $100 sorted by price.

1. SELECT Id, ProductName, UnitPrice


2. FROM Product
3. WHERE UnitPrice NOT BETWEEN 5 AND 100
4. ORDER BY UnitPrice

Results: 4 records.

Id ProductName UnitPrice

33 Geitost 2.50

24 Guaraná Fantástica 4.50

29 Thüringer Rostbratwurst 123.79

38 Côte de Blaye 263.50

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: Get the number of orders and amount sold between Jan 1, 2013 and Jan 31, 2013.

1. SELECT COUNT(Id), SUM(TotalAmount)


2. FROM [Order]
3. WHERE OrderDate BETWEEN '1/1/2013' AND '1/31/2013'

Results:

Count TotalAmount

33 66692.80

SQL WHERE IN Clause

 WHERE IN returns values that matches values in a list or subquery.

 WHERE IN is a shorthand for multiple OR conditions.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL WHERE IN syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE column-name IN (values)

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL WHERE IN Examples

Problem: List all suppliers from the USA, UK, OR Japan

1. SELECT Id, CompanyName, City, Country


2. FROM Supplier
3. WHERE Country IN ('USA', 'UK', 'Japan')

Results: 8 records.

Id CompanyName City Country

1 Exotic Liquids London UK

2 New Orleans Cajun Delights New Orleans USA

3 Grandma Kelly's Homestead Ann Arbor USA

4 Tokyo Traders Tokyo Japan

6 Mayumi's Osaka Japan

8 Specialty Biscuits, Ltd. Manchester UK

16 Bigfoot Breweries Bend USA

19 New England Seafood Cannery Boston USA

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: List all products that are not exactly $10, $20, $30, $40, or $50

1. SELECT Id, ProductName, UnitPrice


2. FROM Product
3. WHERE UnitPrice NOT IN (10,20,30,40,50)

Results: 72 records.

Id ProductName UnitPrice

1 Chai 18.00

2 Chang 19.00

4 Chef Anton's Cajun Seasoning 22.00

5 Chef Anton's Gumbo Mix 21.35

6 Grandma's Boysenberry Spread 25.00

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List all customers that are from


the same countries as the suppliers.

1. SELECT Id, FirstName, LastName, Country


2. FROM Customer
3. WHERE Country IN
4. (SELECT Country
5. FROM Supplier)
Results: 91 records.

Id FirstName LastName Country

1 Maria Anders Germany

4 Thomas Hardy UK

5 Christina Berglund Sweden

6 Hanna Moos Germany

7 Frédérique Citeaux France

SQL WHERE LIKE Statement

 WHERE LIKE determines if a character string matches a pattern.

 Use WHERE LIKE when only a fragment of a text value is known.

 WHERE LIKE supports two wildcard match options: % and _.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL WHERE LIKE syntax


The general syntax is:
1. SELECT column-names
2. FROM table-name
3. WHERE column-name LIKE value

Optional Wildcard characters allowed in 'value' are % (percent) and _ (underscore).


A % matches any string with zero or more characters.
An _ matches any single character.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL WHERE LIKE Examples

Problem: List all products with names that start with 'Ca'

1. SELECT Id, ProductName, UnitPrice, Package


2. FROM Product
3. WHERE ProductName LIKE 'Ca%'

Results: 2 records.

Id ProductName UnitPrice Package

18 Carnarvon Tigers 62.50 16 kg pkg.

60 Camembert Pierrot 34.00 15-300 g rounds

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

Problem: List all products that start with 'Cha' or 'Chan' and have one more character.

1. SELECT Id, ProductName, UnitPrice, Package


2. FROM Product
3. WHERE ProductName LIKE 'Cha_' OR ProductName LIKE 'Chan_'
Results: 2 records.

Id ProductName UnitPrice Package

1 Chai 18.00 10 boxes x 20 bags

2 Chang 19.00 24 - 12 oz bottles

SQL IS NULL Clause

 NULL is a special value that signifies 'no value'.

 Comparing a column to NULL using the = operator is undefined.

 Instead, use WHERE IS NULL or WHERE IS NOT NULL.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL WHERE IS NULL syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE column-name IS NULL

The general not null syntax is:


1. SELECT column-names
2. FROM table-name
3. WHERE column-name IS NOT NULL

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

SQL WHERE IS NULL Examples

Problem: List all suppliers that have no fax number

1. SELECT Id, CompanyName, Phone, Fax


2. FROM Supplier
3. WHERE Fax IS NULL

Results: 16 records

Id CompanyName Phone Fax

1 Exotic Liquids (171) 555-2222 NULL

2 New Orleans Cajun Delights (100) 555-4822 NULL

4 Tokyo Traders (03) 3555-5011 NULL

5 Cooperativa de Quesos 'Las Cabras' (98) 598 76 54 NULL

6 Mayumi's (06) 431-7877 NULL

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
Problem: List all suppliers that do have a fax number

1. SELECT Id, CompanyName, Phone, Fax


2. FROM Supplier
3. WHERE Fax IS NOT NULL

Results: 13 records

Id CompanyName Phone Fax

3 Grandma Kelly's Homestead (313) 555- (313) 555-


5735 3349

7 Pavlova, Ltd. (03) 444- (03) 444-


2343 6588

9 PB Knäckebröd AB 031-987 65 031-987 65


43 91

13 Nord-Ost-Fisch (04721) 8713 (04721) 8714


Handelsgesellschaft mbH

14 Formaggi Fortini s.r.l. (0544) 60323 (0544) 60603

18 Aux joyeux ecclésiastiques (1) (1)


03.83.00.68 03.83.00.62

19 New England Seafood Cannery (617) 555- (617) 555-


3267 3389

21 Lyngbysild 43844108 43844115

22 Zaanse Snoepfabriek (12345) 1212 (12345) 1210

24 G'day, Mate (02) 555- (02) 555-


5914 4873

26 Pasta Buttini s.r.l. (089) (089)


6547665 6547667

28 Gai pâturage 38.76.98.06 38.76.98.58

29 Forêts d'érables (514) 555- (514) 555-


2955 2921
SQL GROUP BY Clause

 The GROUP BY clause groups records into summary rows.

 GROUP BY returns one records for each group.

 GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc.

 GROUP BY can group by one or more columns.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL GROUP BY syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE condition
4. GROUP BY column-names

The general syntax with ORDER BY is:

1. SELECT column-names
2. FROM table-name
3. WHERE condition
4. GROUP BY column-names
5. ORDER BY column-names

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL GROUP BY Examples

Problem: List the number of customers in each country

1. SELECT COUNT(Id), Country


2. FROM Customer
3. GROUP BY Country

Results: 21 records.

Count Country

3 Argentina

2 Austria

2 Belgium

9 Brazil

3 Canada

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List the number of customers in each country sorted high to low

1. SELECT COUNT(Id), Country


2. FROM Customer
3. GROUP BY Country
4. ORDER BY COUNT(Id) DESC

Results: 21 records.

Count Country
13 USA

11 France

11 Germany

9 Brazil

7 UK

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: List the total amount ordered for each customer

1. SELECT SUM(O.TotalPrice), C.FirstName, C.LastName


2. FROM [Order] O JOIN Customer C
3. ON O.CustomerId = C.Id
4. GROUP BY C.FirstName, C.LastName
5. ORDER BY SUM(O.TotalPrice) DESC

This query uses a JOIN with Customer to obtain customer names

Results: 89 records.

Sum FirstName LastName

117483.39 Horst Kloss

115673.39 Jose Pavarotti

113236.68 Roland Mendel

57317.39 Patricia McKenna

52245.90 Paula Wilson

34101.15 Mario Pontes


32555.55 Maria Larsson

SQL HAVING Clause

 HAVING filters records that work on summarized GROUP BY results.

 HAVING applies to summarized group records, whereas WHERE applies to individual records.

 Only the groups that meet the HAVING criteria will be returned.

 HAVING requires that a GROUP BY clause is present.

 WHERE and HAVING can be in the same query.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL HAVING syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE condition
4. GROUP BY column-names
5. HAVING condition

The general syntax with ORDER BY is:

1. SELECT column-names
2. FROM table-name
3. WHERE condition
4. GROUP BY column-names
5. HAVING condition
6. ORDER BY column-names

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL GROUP BY Examples

Problem: List the number of customers in each country. Only include countries with more than 10 customers.

1. SELECT COUNT(Id), Country


2. FROM Customer
3. GROUP BY Country
4. HAVING COUNT(Id) > 10

Results: 3 records

Count Country

11 France

11 Germany

13 USA

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

Problem: List the number of customers in each country, except the USA, sorted high to low.
Only include countries with 9 or more customers.

1. SELECT COUNT(Id), Country


2. FROM Customer
3. WHERE Country <> 'USA'
4. GROUP BY Country
5. HAVING COUNT(Id) >= 9
6. ORDER BY COUNT(Id) DESC
Results: 3 records

Count Country

11 France

11 Germany

9 Brazil

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: List all customer with average orders between $1000 and $1200.

1. SELECT AVG(TotalAmount), FirstName, LastName


2. FROM [Order] O JOIN Customer C ON O.CustomerId = C.Id
3. GROUP BY FirstName, LastName
4. HAVING AVG(TotalAmount) BETWEEN 1000 AND 1200

Results: 10 records

Average FirstName LastName

1081.215000 Miguel Angel


Paolino

1063.420000 Isabel de Castro

1008.440000 Alexander Feuer

1062.038461 Thomas Hardy

1107.806666 Pirkko Koskitalo

1174.945454 Janete Limeira

1073.621428 Antonio Moreno

1065.385000 Rita Müller

1183.010000 José Pedro


Freyre

1057.386666 Carine Schmitt

SQL Alias

 An Alias is a shorthand for a table or column name.

 Aliases reduce the amount of typing required to enter a query.

 Complex queries with aliases are generally easier to read.

 Aliases are useful with JOINs and aggregates: SUM, COUNT, etc.

 An Alias only exists for the duration of the query.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL Alias syntax


The general syntax is:

1. SELECT column-name AS alias-name


2. FROM table-name alias-name
3. WHERE condition

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL Alias Examples

Problem: List total customers in each country.


Display results with easy to understand column headers.

1. SELECT COUNT(C.Id) AS TotalCustomers, C.Country AS Nation


2. FROM Customer C
3. GROUP BY C.Country

TotalCustomers and Nation are column aliases.


The table alias (C) in this example is not particularly useful.

Results: 21 records

TotalCustomers Nation

3 Argentina

2 Austria

2 Belgium

9 Brazil

3 Canada

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: List the total amount ordered by customer


with easy to read column headers
1. SELECT C.Id AS Identifier, C.LastName + ', ' + C.FirstName AS CustomerName,
2. SUM(O.TotalAmount) AS TotalSpent
3. FROM [Order] O JOIN Customer C ON O.CustomerId = C.Id
4. GROUP BY C.Id, C.LastName + ', ' + C.FirstName
5. ORDER BY TotalSpent DESC

The aliases significantly simplify writing the JOIN and ORDER BY clauses.
The C alias in C.Id helps identify the Customer Id rather then the Order Id.

Results: 89 records

Identifier CustomerName TotalSpent

63 Kloss, Horst 117483.39

71 Pavarotti, Jose 115673.39

20 Mendel, Roland 113236.68

37 McKenna, Patricia 57317.39

65 Wilson, Paula 52245.90

SQL JOIN

 A SQL JOIN combines records from two tables.

 A JOIN locates related column values in the two tables.

 A query can contain zero, one, or multiple JOIN operations.

 INNER JOIN is the same as JOIN; the keyword INNER is optional.

The definitive guide


for data professionals
See 2 min video

Different types of JOINs

 (INNER) JOIN: Select records that have matching values in both tables.

 LEFT (OUTER) JOIN: Select records from the first (left-most) table with matching right table records.

 RIGHT (OUTER) JOIN: Select records from the second (right-most) table with matching left table records.

 FULL (OUTER) JOIN: Selects all records that match either left or right table records.

All INNER and OUTER keywords are optional.


Details about the differences between these JOINs are available in subsequent tutorial pages.
SQL JOIN

 A SQL JOIN combines records from two tables.

 A JOIN locates related column values in the two tables.

 A query can contain zero, one, or multiple JOIN operations.

 INNER JOIN is the same as JOIN; the keyword INNER is optional.

The definitive guide


for data professionals

See 2 min video

Different types of JOINs

 (INNER) JOIN: Select records that have matching values in both tables.

 LEFT (OUTER) JOIN: Select records from the first (left-most) table with matching right table records.

 RIGHT (OUTER) JOIN: Select records from the second (right-most) table with matching left table records.

 FULL (OUTER) JOIN: Selects all records that match either left or right table records.
All INNER and OUTER keywords are optional.
Details about the differences between these JOINs are available in subsequent tutorial pages.

Previous

Next

The SQL JOIN syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name1 JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

The general syntax with INNER is:

1. SELECT column-names
2. FROM table-name1 INNER JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

Note: The INNER keyword is optional: it is the default as well as the most commmonly used JOIN operation.

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

SQL JOIN Examples

Problem: List all orders with customer information

1. SELECT OrderNumber, TotalAmount, FirstName, LastName, City, Country


2. FROM [Order] JOIN Customer
3. ON [Order].CustomerId = Customer.Id

In this example using table aliases for [Order] and Customer might have been useful.

Results: 830 records.

OrderNumber TotalAmount FirstName LastName City Country

542378 440.00 Paul Henriot Reims France

542379 1863.40 Karin Josephs Münster Germany

542380 1813.00 Mario Pontes Rio de Brazil


Janeiro

542381 670.80 Mary Saveley Lyon France

542382 3730.00 Pascale Cartrain Charleroi Belgium

542383 1444.80 Mario Pontes Rio de Brazil


Janeiro

542384 625.20 Yang Wang Bern Switzerland

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
ORDERITEM
Id
OrderId
ProductId
UnitPrice
Quantity
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

Problem: List all orders with


product names, quantities, and prices

1. SELECT O.OrderNumber, CONVERT(date,O.OrderDate) AS Date,


2. P.ProductName, I.Quantity, I.UnitPrice
3. FROM [Order] O
4. JOIN OrderItem I ON O.Id = I.OrderId
5. JOIN Product P ON P.Id = I.ProductId
6. ORDER BY O.OrderNumber

This query performs two JOIN operations with 3 tables.


The O, I, and P are table aliases. Date is a column alias.

Results: 2155 records

OrderNumber Date ProductName Quantity UnitPrice

542378 7/4/2012 Queso Cabrales 12 14.00


12:00:00 AM

542378 7/4/2012 Singaporean Hokkien 10 9.80


12:00:00 AM Fried Mee

542378 7/4/2012 Mozzarella di Giovanni 5 34.80


12:00:00 AM

542379 7/5/2012 Tofu 9 18.60


12:00:00 AM

542379 7/5/2012 Manjimup Dried 40 42.40


12:00:00 AM Apples

542380 7/8/2012 Jack's New England 10 7.70


12:00:00 AM Clam Chowder

542380 7/8/2012 Manjimup Dried 35 42.40


12:00:00 AM Apples

542380 7/8/2012 Louisiana Fiery Hot 15 16.80


12:00:00 AM Pepper Sauce
542381 7/8/2012 Gustaf's Knäckebröd 6 16.80
12:00:00 AM

542381 7/8/2012 Ravioli Angelo 15 15.60


12:00:00 AM

542381 7/8/2012 Louisiana Fiery Hot 20 16.80


12:00:00 AM Pepper Sauce

542382 7/9/2012 Sir Rodney's 40 64.80


12:00:00 AM Marmalade

542382 7/9/2012 Geitost 25 2.00


12:00:00 AM

SQL LEFT JOIN

 LEFT JOIN performs a join starting with the first (left-most) table and then any matching second (right-most) table records.

 LEFT JOIN and LEFT OUTER JOIN are the same.

The definitive guide


for data professionals
See 2 min video

Previous

Next

The SQL LEFT JOIN syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name1 LEFT JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

The general LEFT OUTER JOIN syntax is:

1. SELECT OrderNumber, TotalAmount, FirstName, LastName, City, Country


2. FROM Customer C LEFT JOIN [Order] O
3. ON O.CustomerId = C.Id
4. ORDER BY TotalAmount

This will list all customers, whether they placed any order or not.
The ORDER BY TotalAmount shows the customers without orders first (i.e. TotalMount is NULL).

Results: 832 records

OrderNumber TotalAmount FirstName LastName City Country

NULL NULL Diego Roel Madrid Spain

NULL NULL Marie Bertrand Paris France

542912 12.50 Patricio Simpson Buenos Argentina


Aires

542937 18.40 Paolo Accorti Torino Italy


542897 28.00 Pascale Cartrain Charleroi Belgium

542716 28.00 Maurizio Moroni Reggio Italy


Emilia

543028 30.00 Yvonne Moncada Buenos Argentina


Aires

543013 36.00 Fran Wilson Portland USA

SQL RIGHT JOIN

 RIGHT JOIN performs a join starting with the second (right-most) table and then any matching first (left-most) table records.

 RIGHT JOIN and RIGHT OUTER JOIN are the same.

The definitive guide


for data professionals

See 2 min video

Previous

Next
The SQL RIGHT JOIN syntax
The general syntax is:

1. SELECT column-names
2. FROM table-name1 RIGHT JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

The general RIGHT OUTER JOIN syntax is:

1. SELECT column-names
2. FROM table-name1 RIGHT OUTER JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

SQL RIGHT JOIN Example

Problem: List customers that have not placed orders

1. SELECT TotalAmount, FirstName, LastName, City, Country


2. FROM [Order] O RIGHT JOIN Customer C
3. ON O.CustomerId = C.Id
4. WHERE TotalAmount IS NULL

This returns customers that, when joined, have no matching order.

Results: 2 records

TotalAmount FirstName LastName City Country

NULL Diego Roel Madrid Spain

NULL Marie Bertrand Paris France


SQL FULL JOIN Statement

 FULL JOIN returns all matching records from both tables whether the other table matches or not.

 FULL JOIN can potentially return very large datasets.

 FULL JOIN and FULL OUTER JOIN are the same.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL FULL JOIN syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name1 FULL JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

The general FULL OUTER JOIN syntax is:


1. SELECT column-names
2. FROM table-name1 FULL OUTER JOIN table-name2
3. ON column-name1 = column-name2
4. WHERE condition

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL FULL JOIN Examples

Problem: Match all customers and suppliers by country

1. SELECT C.FirstName, C.LastName, C.Country AS CustomerCountry,


2. S.Country AS SupplierCountry, S.CompanyName
3. FROM Customer C FULL JOIN Supplier S
4. ON C.Country = S.Country
5. ORDER BY C.Country, S.Country

This returns suppliers that have no customers in their country,


and customers that have no suppliers in their country,
and customers and suppliers that are from the same country.

Results: 195 records

FirstName LastName CustomerCountry SupplierCountry CompanyName

NULL NULL NULL Australia Pavlova, Ltd.

NULL NULL NULL Australia G'day, Mate

NULL NULL NULL Japan Tokyo Traders

NULL NULL NULL Japan Mayumi's

NULL NULL NULL Netherlands Zaanse


Snoepfabriek
NULL NULL NULL Singapore Leka Trading

Patricio Simpson Argentina NULL NULL

Yvonne Moncada Argentina NULL NULL

Sergio Gutiérrez Argentina NULL NULL

Georg Pipps Austria NULL NULL

Roland Mendel Austria NULL NULL

Pascale Cartrain Belgium NULL NULL

Catherine Dewey Belgium NULL NULL

Bernardo Batista Brazil Brazil Refrescos


Americanas
LTDA

Lúcia Carvalho Brazil Brazil Refrescos


Americanas
LTDA

Janete Limeira Brazil Brazil Refrescos


Americanas
LTDA

Aria Cruz Brazil Brazil Refrescos


Americanas
LTDA

André Fonseca Brazil Brazil Refrescos


Americanas
LTDA

Mario Pontes Brazil Brazil Refrescos


Americanas
LTDA

Pedro Afonso Brazil Brazil Refrescos


Americanas
LTDA

Paula Parente Brazil Brazil Refrescos


Americanas
LTDA
Anabela Domingues Brazil Brazil Refrescos
Americanas
LTDA

Elizabeth Lincoln Canada Canada Ma Maison

Elizabeth Lincoln Canada Canada Forêts d'érables

Yoshi Tannamuri Canada Canada Ma Maison

Yoshi Tannamuri Canada Canada Forêts d'érables

Jean Fresnière Canada Canada Ma Maison

SQL Self JOIN

 A self JOIN occurs when a table takes a 'selfie'.

 A self JOIN is a regular join but the table is joined with itself.

 This can be useful when modeling hierarchies.

 They are also useful for comparisons within a table.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL Self JOIN syntax


The general syntax is:
1. SELECT column-names
2. FROM table-name T1 JOIN table-name T2
3. WHERE condition

T1 and T2 are different table aliases for the same table

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL Self JOIN Examples

Problem: Match customers that are from the same city and country

1. SELECT B.FirstName AS FirstName1, B.LastName AS LastName1,


2. A.FirstName AS FirstName2, A.LastName AS LastName2,
3. B.City, B.Country
4. FROM Customer A, Customer B
5. WHERE A.Id <> B.Id
6. AND A.City = B.City
7. AND A.Country = B.Country
8. ORDER BY A.Country

A and B are aliases for the same Customer table.

Results: 88 records

FirstName1 LastName1 FirstName2 LastName2 City Country

Patricio Simpson Yvonne Moncada Buenos Aires Argentina

Patricio Simpson Sergio Gutiérrez Buenos Aires Argentina

Yvonne Moncada Patricio Simpson Buenos Aires Argentina

Yvonne Moncada Sergio Gutiérrez Buenos Aires Argentina

Sergio Gutiérrez Patricio Simpson Buenos Aires Argentina

Sergio Gutiérrez Yvonne Moncada Buenos Aires Argentina


Anabela Domingues Lúcia Carvalho Sao Paulo Brazil

Anabela Domingues Aria Cruz Sao Paulo Brazil

Anabela Domingues Pedro Afonso Sao Paulo Brazil

Bernardo Batista Janete Limeira Rio de Janeiro Brazil

Bernardo Batista Mario Pontes Rio de Janeiro Brazil

Lúcia Carvalho Anabela Domingues Sao Paulo Brazil

Lúcia Carvalho Aria Cruz Sao Paulo Brazil

Lúcia Carvalho Pedro Afonso Sao Paulo Brazil

Janete Limeira Bernardo Batista Rio de Janeiro Brazil

Janete Limeira Mario Pontes Rio de Janeiro Brazil

Aria Cruz Anabela Domingues Sao Paulo Brazil

Aria Cruz Lúcia Carvalho Sao Paulo Brazil

Aria Cruz Pedro Afonso Sao Paulo Brazil

Mario Pontes Bernardo Batista Rio de Janeiro Brazil

Mario Pontes Janete Limeira Rio de Janeiro Brazil

Pedro Afonso Anabela Domingues Sao Paulo Brazil

Pedro Afonso Lúcia Carvalho Sao Paulo Brazil

Pedro Afonso Aria Cruz Sao Paulo Brazil

Dominique Perrier Marie Bertrand Paris France

Marie Bertrand Dominique Perrier Paris France

Janine Labrune Carine Schmitt Nantes France

Carine Schmitt Janine Labrune Nantes France

SQL UNION Clause

 UNION combines the result sets of two queries.

 Column data types in the two queries must match.


 UNION combines by column position rather than column name.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL UNION syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. UNION
4. SELECT column-names
5. FROM table-name

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL UNION Examples

Problem: List all contacts, i.e., suppliers and customers.

1. SELECT 'Customer' As Type,


2. FirstName + ' ' + LastName AS ContactName,
3. City, Country, Phone
4. FROM Customer
5. UNION
6. SELECT 'Supplier',
7. ContactName, City, Country, Phone
8. FROM Supplier

This is a simple example in which the table alias would be useful

Results:

Type ContactName City Country Phone

Customer Alejandra Camino Madrid Spain (91) 745


6200

Customer Alexander Feuer Leipzig Germany 0342-023176

Customer Ana Trujillo México D.F. Mexico (5) 555-4729

Customer Anabela Domingues Sao Paulo Brazil (11) 555-


2167

Supplier Anne Heikkonen Lappeenranta Finland (953) 10956

Supplier Antonio del Valle Oviedo Spain (98) 598 76


Saavedra 54

Supplier Beate Vileid Sandvika Norway (0)2-953010


Supplier Carlos Diaz Sao Paulo Brazil (11) 555
4640

Supplier Chandra Leka Singapore Singapore 555-8787

Supplier Chantal Goulet Ste- Canada (514) 555-


Hyacinthe 2955

Supplier Charlotte Cooper London UK (171) 555-


2222

SQL Subqueries

 A subquery is a SQL query within a query.

 Subqueries are nested queries that provide data to the enclosing query.

 Subqueries can return individual values or a list of records

 Subqueries must be enclosed with parenthesis

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL subquery syntax


There is no general syntax; subqueries are regular queries placed inside parenthesis.
Subqueries can be used in different ways and at different locations inside a query:
Here is an subquery with the IN operator

1. SELECT column-names
2. FROM table-name1
3. WHERE value IN (SELECT column-name
4. FROM table-name2
5. WHERE condition)

Subqueries can also assign column values for each record:

1. SELECT column1 = (SELECT column-name FROM table-name WHERE condition),


2. column-names
3. FROM table-name
4. WEHRE condition

ORDERITEM
Id
OrderId
ProductId
UnitPrice
Quantity
PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL Subquery Examples

Problem: List products with order quantities greater than 100.

1. SELECT ProductName
2. FROM Product
3. WHERE Id IN (SELECT ProductId
4. FROM OrderItem
5. WHERE Quantity > 100)

Results: 12 records

PoductName

Guaraná Fantástica

Schoggi Schokolade

Chartreuse verte

Jack's New England Clam


Chowder

Rogede sild

Manjimup Dried Apples

Perth Pasties

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

SQL Subquery Examples

Problem: List all customers with their total number of orders

1. SELECT FirstName, LastName,


2. OrderCount = (SELECT COUNT(O.Id) FROM [Order] O WHERE O.CustomerId = C.Id)
3. FROM Customer C

This is a correlated subquery because the subquery references the enclosing query (i.e. the C.Id in the WHERE clause).

Results: 91 records

FirstName LastName OrderCount

Maria Anders 6

Ana Trujillo 4

Antonio Moreno 7

Thomas Hardy 13

Christina Berglund 18
Hanna Moos 7

Frédérique Citeaux 11

Martín Sommer 3

SQL WHERE ANY, ALL Clause

 ANY and ALL keywords are used with a WHERE or HAVING clause.

 ANY and ALL operate on subqueries that return multiple values.

 ANY returns true if any of the subquery values meet the condition.

 ALL returns true if all of the subquery values meet the condition.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL WHERE ANY and ALL syntax


The general ANY syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE column-name operator ANY
4. (SELECT column-name
5. FROM table-name
6. WHERE condition)

The general ALL syntax is:


1. SELECT column-names
2. FROM table-name
3. WHERE column-name operator ALL
4. (SELECT column-name
5. FROM table-name
6. WHERE condition)

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
ORDERITEM
Id
OrderId
ProductId
UnitPrice
Quantity

SQL ANY Example

Problem: Which products were sold by the unit (i.e. quantity = 1)

1. SELECT ProductName
2. FROM Product
3. WHERE Id = ANY
4. (SELECT ProductId
5. FROM OrderItem
6. WHERE Quantity = 1)

Results: 17 records

ProductName

Chef Anton's Cajun Seasoning

Grandma's Boysenberry Spread

Uncle Bob's Organic Dried Pears

Ikura

Konbu

Tofu

Teatime Chocolate Biscuits


Sir Rodney's Marmalade

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount

SQL ALL Example

Problem: List customers who placed orders that are


larger than the average of each customer order

1. SELECT DISTINCT FirstName + ' ' + LastName as CustomerName


2. FROM Customer, [Order]
3. WHERE Customer.Id = [Order].CustomerId
4. AND TotalAmount > ALL
5. (SELECT AVG(TotalAmount)
6. FROM [Order]
7. GROUP BY CustomerId)

Results: 22 records

CustomerName

Art Braunschweiger

Christina Berglund

Elizabeth Lincoln

Frédérique Citeaux

Georg Pipps

Horst Kloss

Howard Snyder
SQL WHERE EXISTS Statement

 WHERE EXISTS tests for the existence of any records in a subquery.

 EXISTS returns true if the subquery returns one or more records.

 EXISTS is commonly used with correlated subqueries.

The definitive guide


for data professionals

See 2 min video

Previous

Next

The SQL EXISTS syntax


The general syntax is:

1. SELECT column-names
2. FROM table-name
3. WHERE EXISTS
4. (SELECT column-name
5. FROM table-name
6. WHERE condition)

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL EXISTS Example

Problem: Find suppliers with products over $100.

1. SELECT CompanyName
2. FROM Supplier
3. WHERE EXISTS
4. (SELECT ProductName
5. FROM Product
6. WHERE SupplierId = Supplier.Id
7. AND UnitPrice > 100)

This is a correlated subquery because the subquery references the enclosing query (with Supplier.Id).

Results: 2 records

CompanyName

Plutzer Lebensmittelgroßmärkte AG

Aux joyeux ecclésiastiques

SQL SELECT INTO Statement

 SELECT INTO copies data from one table into a new table.

 SELECT INTO creates a new table located in the default filegroup.

The definitive guide


for data professionals
See 2 min video

Previous

Next

The SQL SELECT INTO syntax


The general syntax is:

1. SELECT column-names
2. INTO new-table-name
3. FROM table-name
4. WHERE EXISTS
5. (SELECT column-name
6. FROM table-name
7. WHERE condition)

The new table will have column names as specified in the query.

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

SQL SELECT INTO Example

Problem: Copy all suppliers from USA to a new SupplierUSA table.

1. SELECT * INTO SupplierUSA


2. FROM Supplier
3. WHERE Country = 'USA'
Results: 4 rows affected

Here are the records in the newly created table SupplierUSA:

Id CompanyName ContactName City Country Phone Fax

2 New Orleans Shelley New USA (100) NULL


Cajun Delights Burke Orleans 555-
4822

3 Grandma Kelly's Regina Ann USA (313) (313)


Homestead Murphy Arbor 555- 555-
5735 3349

16 Bigfoot Breweries Cheryl Saylor Bend USA (100) NULL


555-
4822

19 New England Robb Boston USA (617) (617)


Seafood Cannery Merchant 555- 555-
3267 3389

SQL INSERT INTO SELECT Statement

 INSERT INTO SELECT copies data from one table to another table.

 INSERT INTO SELECT requires that data types in source and target tables match.

The definitive guide


for data professionals

See 2 min video

Previous

Next
The SQL INSERT INTO SELECT syntax
The general syntax is:

1. INSERT INTO table-name (column-names)


2. SELECT column-names
3. FROM table-name
4. WHERE condition

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax

SQL INSERT SELECT INTO

Problem: Copy all Canadian suppliers into the Customer table

1. INSERT INTO Customer (FirstName, LastName, City, Country, Phone)


2. SELECT LEFT(ContactName, CHARINDEX(' ',ContactName) - 1) AS FirstName,
3. SUBSTRING(ContactName, CHARINDEX(' ',ContactName) + 1, 100) AS LastName,
4. City, Country, Phone
5. FROM Supplier
6. WHERE Country = 'Canada'

LEFT, CHARINDEX, and SUBSTRING are built-in functions.

Results: 2 rows affected.

These are the two new Customer records

FirstName LastName City Country Phone

Jean-Guy Lauzon Montréal Canada (514) 555-9022

Chantal Goulet Ste-Hyacinthe Canada (514) 555-2955


tored Procedure: Stored Procedure in SQL Server can be defined as the set of logical group of SQL statements which are grouped to perform a specific task. There are many benefits of using a
stored procedure. The main benefit of using a stored procedure is that it increases the performance of the database.The other benefits of using the Stored Procedure are given below.

Benefits of Using the Stored Procedure


1. One of the main benefits of using the Stored procedure is that it reduces the amount of information sent to the database server. It can become a more important benefit when the bandwidth of
the network is less. Since if we send the SQL query (statement) which is executing in a loop to the server through network and the network gets disconnected, then the execution of the SQL
statement doesn't return the expected results, if the SQL query is not used between Transaction statement and rollback statement is not used.
2. Compilation step is required only once when the stored procedure is created. Then after it does not require recompilation before executing unless it is modified and reutilizes the same
execution plan whereas the SQL statements need to be compiled every time whenever it is sent for execution even if we send the same SQL statement every time.
3. It helps in re usability of the SQL code because it can be used by multiple users and by multiple clients since we need to just call the stored procedure instead of writing the same SQL statement
every time. It helps in reducing the development time.
4. Stored procedure is helpful in enhancing the security since we can grant permission to the user for executing the Stored procedure instead of giving permission on the tables used in the Stored
procedure.
5. Sometimes, it is useful to use the database for storing the business logic in the form of stored procedure since it makes it secure and if any change is needed in the business logic, then we may
only need to make changes in the stored procedure and not in the files contained on the web server.

How to Write a Stored Procedure in SQL Server

Suppose there is a table called tbl_Students whose structure is given below:

Hide Copy Code

CREATE TABLE tbl_Students

(
[Studentid] [int] IDENTITY(1,1) NOT NULL,
[Firstname] [nvarchar](200) NOT NULL,
[Lastname] [nvarchar](200) NULL,
[Email] [nvarchar](100) NULL
)

Support we insert the following data into the above table:

Hide Copy Code

Insert into tbl_Students (Firstname, lastname, Email)


Values('Vivek', 'Johari', 'vivek@abc.com')

Insert into tbl_Students (Firstname, lastname, Email)


Values('Pankaj', 'Kumar', 'pankaj@abc.com')

Insert into tbl_Students (Firstname, lastname, Email)


Values('Amit', 'Singh', 'amit@abc.com')

Insert into tbl_Students (Firstname, lastname, Email)


Values('Manish', 'Kumar', 'manish@abc.comm')

Insert into tbl_Students (Firstname, lastname, Email)


Values('Abhishek', 'Singh', 'abhishek@abc.com')

Now, while writing a Stored Procedure, the first step will be to write the Create Procedure statement as the first statement:

Hide Copy Code

Create Procedure Procedure-name


(
Input parameters ,
Output Parameters (If required)
)
As
Begin
Sql statement used in the stored procedure
End

Now, suppose we need to create a Stored Procedure which will return a student name whose studentid is given as the input parameter to the stored procedure. Then,
the Stored Procedure will be:

Hide Copy Code

/* Getstudentname is the name of the stored procedure*/

Create PROCEDURE Getstudentname(

@studentid INT --Input parameter , Studentid of the student

)
AS
BEGIN
SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid
END

We can also collect the student name in the output parameter of the Stored Procedure. For example:

Hide Copy Code

/*
GetstudentnameInOutputVariable is the name of the stored procedure which
uses output variable @Studentname to collect the student name returns by the
stored procedure
*/

Create PROCEDURE GetstudentnameInOutputVariable


(

@studentid INT, --Input parameter , Studentid of the student


@studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT keyword
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid
END

Note:-/* */ is used to write comments in one or multiple lines

-- is used to write a comment in a single line

How to Alter a Stored Procedure in a SQL Server

Alter keyword. Now if we want to getstudent email address through the same
In SQL Server, a stored procedure can be modified with the help of the

procedure GetstudentnameInOutputVariable . So we need to modify it by adding one more output parameter " @StudentEmail "
which is shown below:

Hide Copy Code

/*
Stored Procedure GetstudentnameInOutputVariable is modified to collect the
email address of the student with the help of the Alert Keyword
*/

Alter PROCEDURE GetstudentnameInOutputVariable


(

@studentid INT, --Input parameter , Studentid of the student


@studentname VARCHAR (200) OUT, -- Output parameter to collect the student name
@StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname,
@StudentEmail=email FROM tbl_Students WHERE studentid=@studentid
END

Note: It is not necessary that a stored procedure will have to return. There can be a case when a stored procedure doesn't returns anything. For example, a stored procedure can be used

to Insert, delete orupdate a SQL statement. For example, the below stored procedure is used to insert value into the table tbl_students.

Hide Copy Code

/*
This Stored procedure is used to Insert value into the table tbl_students.
*/

Create Procedure InsertStudentrecord


(
@StudentFirstName Varchar(200),
@StudentLastName Varchar(200),
@StudentEmail Varchar(50)
)
As
Begin
Insert into tbl_Students (Firstname, lastname, Email)
Values(@StudentFirstName, @StudentLastName,@StudentEmail)
End

Execution of the Stored Procedure in SQL Server


Execution of the Stored Procedure which doesn't have an Output Parameter

Execute" or "Exec" Keyword. For example, if we want to execute the stored procedure
A stored procedure is used in the SQL Server with the help of the "

Getstudentname", then we will use the following statement.


"

Hide Copy Code

Execute Getstudentname 1
Exec Getstudentname 1

Execution of the Stored Procedure using the Output Parameter

If we want to execute the Stored procedure " GetstudentnameInOutputVariable " , then we first need to declare the variable to collect the output values.
For example:

Hide Copy Code

Declare @Studentname as nvarchar(200) -- Declaring the variable to collect the Studentname


Declare @Studentemail as nvarchar(50) -- Declaring the variable to collect the
Studentemail
Execute GetstudentnameInOutputVariable 1 , @Studentname output, @Studentemail output
select @Studentname,@Studentemail -- "Select" Statement is used to show the output from
Procedure

Summary

In the end, we can say that a Stored procedure not only enhances the possibility of reusing the code and execution plan, but it also increases the performance of the database by reducing the
traffic of the network by reducing the amount of information sent over the network.

Here, we will see how to create select, insert, update, delete statements using stored procedure. Let's take a look at a practical example. We create a table.

Creating Table

1. CREATE TABLE employee(


2.
3. id INTEGER NOT NULL PRIMARY KEY,
4.
5. first_name VARCHAR(10),
6.
7. last_name VARCHAR(10),
8.
9. salary DECIMAL(10,2),
10.
11. city VARCHAR(20),
12.
13. )

Now insert some values in the table and using select statement to select a table.

1. INSERT INTO employee VALUES (2, 'Monu', 'Rathor',4789,'Agra');


2.
3. GO
4.
5. INSERT INTO employee VALUES (4, 'Rahul' , 'Saxena', 5567,'London');
6.
7. GO
8.
9. INSERT INTO employee VALUES (5, 'prabhat', 'kumar', 4467,'Bombay');
10.
11. go
12.
13. INSERT INTO employee VALUES (6, 'ramu', 'kksingh', 3456, 'jk');
14.
15. go
16.
17. select * from employee

Table looks like this.

Figure 1

Stored procedure for Select, insert, update, delete

Here, we create a stored procedure for select,insert,update,delete statements to select the data from the table.

1. Alter PROCEDURE MasterInsertUpdateDelete


2. (
3. @id INTEGER,
4. @first_name VARCHAR(10),
5. @last_name VARCHAR(10),
6. @salary DECIMAL(10,2),
7. @city VARCHAR(20),
8. @StatementType nvarchar(20) = ''
9. )
10. AS
11. BEGIN
12. IF @StatementType = 'Insert'
13. BEGIN
14. insert into employee (id,first_name,last_name,salary,city) values( @id, @first_name, @last_name, @salary, @city)
15. END
16. IF @StatementType = 'Select'
17. BEGIN
18. select * from employee
19. END
20. IF @StatementType = 'Update'
21. BEGIN
22. UPDATE employee SET
23. First_name = @first_name, last_name = @last_name, salary = @salary,
24. city = @city
25. WHERE id = @id
26. END
27. else IF @StatementType = 'Delete'
28. BEGIN
29. DELETE FROM employee WHERE id = @id
30. END
31. end

Now press F5 to execute the stored procedure.

Now open object explorer and select storeprocedure MasterInsertUpdateDelete.

Stored Procedure to Check Insert

StatementType = 'Insert'

MasterInsertUpdateDelete -> right click select execute stored procedure...


Figure2

Execute procedure window will be open.


Figure3

Now for insert we fill the data in required field.

StatementType=insert
Figure4

Click on the ok Button. and check in the employee table with following inserted data.
Figure5

Stored Procedure to Check update

MasterInsertUpdateDelete -> right click select execute stored procedure...

Execute procedure window will be open.

StatementType = 'Update'

Figure6

Click on the ok Button. and check in the employee table with following updated data where id is 7.
Figure7

Stored Procedure to Check Delete

MasterInsertUpdateDelete -> right click select execute stored procedure...

Execute procedure window will be open.

StatementType = 'Delete'

Figure8

we delete record from table which has id=2

Click on the ok Button. and check in the employee table with following deleted data where id is 2.
Figure9

You might also like