0% found this document useful (0 votes)
42 views26 pages

View Joins and Aggregate FN

The document discusses various SQL concepts including views, functions, joins, grouping, and copying data between tables. Views are virtual tables that display data from real tables. Functions like AVG(), COUNT(), and ROUND() return aggregated or transformed values from a column. Joins combine data from multiple tables. SELECT INTO copies data to a new table, while INSERT INTO SELECT copies between existing tables.

Uploaded by

Aman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views26 pages

View Joins and Aggregate FN

The document discusses various SQL concepts including views, functions, joins, grouping, and copying data between tables. Views are virtual tables that display data from real tables. Functions like AVG(), COUNT(), and ROUND() return aggregated or transformed values from a column. Joins combine data from multiple tables. SELECT INTO copies data to a new table, while INSERT INTO SELECT copies between existing tables.

Uploaded by

Aman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SQL 

Views

• A view is a virtual table.


• A view contains rows and columns, just like a real table. The fields in a
view are fields from one or more real tables in the database.
• SQL CREATE VIEW Syntax
• CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
• Select * from view name
SQL Functions- Aggregate Functions

• SQL aggregate functions return a single value, calculated from values in a


column.
• Useful aggregate functions:
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• FIRST() - Returns the first value
• LAST() - Returns the last value
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
SQL Scalar functions

• SQL scalar functions return a single value, based on the input value.
• Useful scalar functions:
• UCASE() - Converts a field to upper case
• LCASE() - Converts a field to lower case
• MID() - Extract characters from a text field
• LEN() - Returns the length of a text field
• ROUND() - Rounds a numeric field to the number of decimals specified
• NOW() - Returns the current system date and time
• FORMAT() - Formats how a field is to be displayed
• SELECT AVG(Price) AS PriceAverage FROM Products;
• SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM
Orders
WHERE CustomerID=7;

• SELECT MAX(Price) AS HighestPrice FROM Products;


SQL GROUP BY Statement

• The GROUP BY statement is used in conjunction with the aggregate


functions to group the result-set by one or more columns.

• SQL GROUP BY Syntax


• SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
• SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS
NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
SQL HAVING Clause

• The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.

• SQL HAVING Syntax


• SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
• Example
• SELECT Employees.LastName, COUNT(Orders.OrderID) AS
NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
• Example
• SELECT UCASE(CustomerName) AS Customer, City
FROM Customers;

• Example
• SELECT ProductName, ROUND(Price,0) AS RoundedPrice
FROM Products;
SQL Joins
• An SQL JOIN clause is used to combine rows from two or more tables,
based on a common field between them.
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20

CustomerI CustomerName ContactName Country


D
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Ana Trujillo Mexico
Emparedados y
helados
3 Antonio Moreno Antonio Moreno Mexico
Taquería
• Example
• SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

OrderID CustomerName OrderDate


10308 Ana Trujillo Emparedados y helados 9/18/1996
10365 Antonio Moreno Taquería 11/27/199
6
10383 Around the Horn 12/16/199
6
10355 Around the Horn 11/15/199
6
10278 Berglunds snabbköp 8/12/1996
Different SQL JOINs

• 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
SQL INNER JOIN Keyword

• The INNER JOIN keyword selects all rows from both tables as long as
there is a match between the columns in both tables.

SQL INNER JOIN Syntax


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
CustomerID CustomerN ContactNa Address City PostalCode Country
ame me
1 Alfreds Maria Obere Str. Berlin 12209 Germany
Futterkiste Anders 57

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedad Constitució
os y n 2222
helados
3 Antonio Antonio Mataderos México D.F. 05023 Mexico
Moreno Moreno 2312
Taquería

OrderID CustomerID EmployeeID OrderDate ShipperID


10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
SQL INNER JOIN Example

• Example
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL LEFT JOIN Keyword

• The LEFT JOIN keyword returns all rows from the left table (table1), with
the matching rows in the right table (table2). The result is NULL in the
right side when there is no match.

SQL LEFT JOIN Syntax


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;
SQL LEFT JOIN Example

• Example
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL RIGHT JOIN Keyword

• The RIGHT JOIN keyword returns all rows from the right table (table2),
with the matching rows in the left table (table1). The result is NULL in the
left side when there is no match.

SQL RIGHT JOIN Syntax


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;
SQL RIGHT JOIN Example

• Example
• SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;
SQL FULL OUTER JOIN Keyword

• The FULL OUTER JOIN keyword returns all rows from the left table
(table1) and from the right table (table2).
• The FULL OUTER JOIN keyword combines the result of both LEFT and
RIGHT joins.

SQL FULL OUTER JOIN Syntax


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
SQL FULL OUTER JOIN Example

• SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL SELECT INTO Statement

• With SQL, you can copy information from one table into new table.
• The SELECT INTO statement copies data from one table and inserts it
into a new table.
SQL SELECT INTO Syntax
We can copy all columns into the new table:
SELECT *
INTO newtable [IN externaldb]
FROM table1;
Or we can copy only the columns we
want into the new table:
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
The new table will be created with the column-names
and types as defined in the SELECT statement. You
can apply new names using the AS clause.
SQL SELECT INTO Examples
Create a backup copy of Customers:
SELECT *
INTO CustomersBackup2013
FROM Customers;

Copy only a few columns into the new table:


SELECT CustomerName, ContactName
INTO CustomersBackup2013
FROM Customers;
SQL INSERT INTO SELECT Statement

• 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

SQL INSERT INTO SELECT Syntax


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:
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
SQL INSERT INTO SELECT Examples

Example
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;

Example
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';

You might also like