Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Introduction to Oracle9i: SQL1 SQL Group Functions.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Mary K. Olson PS Reporting Instance – Query Tool 101.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Microsoft Access 2010 Chapter 7 Using SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
PeopleSoft Financials Advanced Query Training Financial Information Systems and Reporting Controller’s Division
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
From: SQL From:
Structured Query Language
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Structured Query Language (Data Manipulation Language)
Chapter 3 Introduction to SQL(3)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
JOINS (Joinining multiple tables)
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Access: SQL Participation Project
Structured Query Language
SQL Aggregation.
Structured Query Language – The Fundamentals
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
Query Functions.
Section 4 - Sorting/Functions
Joins and other advanced Queries
Set Operations Union Intersect Minus.
Aggregate Functions.
JOINS (Joinining multiple tables)
Shelly Cashman: Microsoft Access 2016
Group Operations Part IV.
Presentation transcript:

Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI

1.IN Operator The "Persons" table: P_IdFirstNameLastNameAddressCity 1AliZafarAfghanistanKabul 2UsmanKhanPakistanIslamabad 3NasemGulIndiaDelhi

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')

2.SQL INNER JOIN O_IdOrderNoP_Id The "Orders" table: P_IdFirstNameLastNameAddressCity 1AliZafarAfghanistanKabul 2UsmanKhanPakistanIslamabad 3NasemGulIndiaDelhi The "Persons" table:

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

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).

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).

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.

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

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’

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

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'

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

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

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 York Boston Chicago San Diego San Jose4003 Table: BRANCH

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

Here is the result. region_nbr count (branch_nbr) sum (employee_count) min (employee_count) max (employee_count) avg (employee_count)