Download presentation
Presentation is loading. Please wait.
Published byJulie Fox Modified over 9 years ago
1
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI
2
1.IN Operator The "Persons" table: P_IdFirstNameLastNameAddressCity 1AliZafarAfghanistanKabul 2UsmanKhanPakistanIslamabad 3NasemGulIndiaDelhi
3
IN Operator Now we want to select the persons with a last name equal to “Zafar" or “Gul" from the table above. We use the following SELECT statement: SELECT * FROM Persons WHERE LastName IN (' Zafar’, ‘Gul')
4
2.SQL INNER JOIN O_IdOrderNoP_Id 1778953 2446783 3224561 4245621 53476415 The "Orders" table: P_IdFirstNameLastNameAddressCity 1AliZafarAfghanistanKabul 2UsmanKhanPakistanIslamabad 3NasemGulIndiaDelhi The "Persons" table:
5
SQL INNER JOIN Now we want to list all the persons with any orders. The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in "Persons" that do not have matches in "Orders", those rows will NOT be listed. We use the following SELECT statement: SELECT Persons.LastName, Persons.FirstName,Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName
6
3.SQL LEFT JOIN Now we want to list all the persons and their orders - if any, from the tables above. We use the following SELECT statement: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders).
7
4.SQL RIGHT JOIN Now we want to list all the orders with containing persons - if any, from the tables above. We use the following SELECT statement: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons).
8
5.The TOP Clause The TOP clause is used to specify the number of records to return. The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance. Note: Not all database systems support the TOP clause.
9
The TOP Clause SELECT * FROM Persons LIMIT 5 SELECT * FROM Persons WHERE ROWNUM <=5 we want to select only the two first records in the table above. SELECT TOP 2 * FROM Persons we want to select only 50% of the records in the table above. SELECT TOP 50 PERCENT * FROM Persons
10
6.SQL BETWEEN The BETWEEN operator is used in a WHERE clause to select a range of data between two values. The values can be numbers, text, or dates. SELECT * FROM Persons WHERE Age BETWEEN ‘18’ AND ‘22’
11
7.SQL SELECT DISTINCT Statement In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. SELECT DISTINCT City FROM Persons
12
8.SQL LIKE The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. we want to select the persons living in a city that starts with “K" from the table Persons. We use the following SELECT statement: SELECT * FROM Persons WHERE City LIKE ‘K%‘ we want to select the persons living in a city that ends with an “i" from the "Persons" table. We use the following SELECT statement: SELECT * FROM Persons WHERE City LIKE '%i'
13
9.SQL CREATE DATABASE The CREATE DATABASE statement is used to create a database. Now we want to create a database called “mihe". We use the following CREATE DATABASE statement: CREATE DATABASE mihe
14
What are SQL Aggregate Functions? The SQL Aggregate Functions are functions that provide mathematical operations. If you need to add, count or perform basic statistics, these functions will be of great help. The SQL Aggregate Functions are useful when mathematical operations must be performed on all or a grouping of values. The functions include: count() - counts a number of rows sum() - compute sum avg() - compute average min() - compute minimum max() - compute maximum
15
SQL Aggregate Functions Example The following example Aggregate Functions are applied to the employee_count of the branch table. The region_nbr is the level of grouping. branch_nbrbranch_nameregion_nbremployee_count 108New York10010 110Boston1006 212Chicago2005 404San Diego4006 415San Jose4003 Table: BRANCH
16
SQL Aggregate Functions Example This SQL Statement with aggregate functions is executed: SELECT region_nbr, count (branch_nbr), sum(employee_count), min(employee_count), max(employee_count), avg(employee_count) FROM branch GROUP BY region_nbr ORDER BY region_nbr
17
Here is the result. region_nbr count (branch_nbr) sum (employee_count) min (employee_count) max (employee_count) avg (employee_count) 1002166108 20015555 40029364
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.