SlideShare a Scribd company logo
Dynamic Websites
PHP with Oracle DB   By Belal Arfa
Section 2
In this section will discuss
1- Examples on last section
2- Quiz
3- Like Operator
4- Top Stmt
5- Union VS Union ALL
6- IN Operator
7- Aggregation Functions
8- Group By
9- Having
10- Difference Between Having and Where
Quiz (1)
• Relations:
Movie(title, year, length, inColor, studioName, producerC#)
StarsIn(movieTitle, movieYear, starName)
MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth)
Studio(name, address, presC#)
• Queries:
a) Find the address of MGM studios.
b) Find Sandra Bullock’s birthdate.
c) Find all the stars that appear either in a movie made in 1980 or a movie
with “Love” in the title.
d) Find all executives worth at least $10,000,000.
e) Find all the stars who either are male or live in Miami ( have Miami as a
part of their address).
Quiz (1) Answer
a) SELECT address FROM studio
    WHERE name = ‘MGM’;
b) SELECT birthdate FROM moviestar
    WHERE name = ‘Sandra Bullock’;
c) SELECT starName FROM StarsIn
    WHERE movieYear = 1980 OR movieTitle LIKE ‘%Love%’;
d) SELECT name FROM MovieExec
    WHERE netWorth >= 10,000,000;
e) SELECT name FROM MovieStar
    WHERE gender = ‘M’ OR address LIKE ‘% Miami %’;
LIKE Operator
• The LIKE operator is used to search for a specified pattern in a column..
• SQL LIKE Syntax:
– SELECT column_name(s) FROM table_name
     WHERE column_name LIKE pattern
Persons Table
          P_ID          LastName         FirstName        Address             City

            1            Hansen             Ola         Timoteivn 10      Sandnes

            2           Svendson           Tove          Borgvn 23        Sandnes

            3            Pettersen          Kari          Storgt 20       Stavanger
Ex. Want to select persons living in city ending with "s" from "Persons" table. (%
can be used to define wildcards (missing letters in pattern))
Sol: SELECT * FROM Persons WHERE City LIKE ‘%s';
SELECT TOP Stmt
 TOP clause is used to specify the number of records to return.
 Can be useful on large tables with ‘000s of records as Returning a
large
number of records can impact on performance.
 SQL TOP Syntax:
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number
Ex : Want to select only the two first records in the table above
Sol: SELECT * FROM Persons Where RowNum<2
Union VS Union ALL

The UNION operator returns only distinct rows
that appear in either result,
while the UNION ALL operator returns all rows.

The UNION ALL operator does not eliminate
duplicate selected rows.
IN Operator Nested Queries
– SELECT Model FROM Product
WHERE ManufacturerID IN
(SELECT ManufacturerID FROM Manufacturer
WHERE Manufacturer = 'Dell')
• The nested query above will select all models from the “Product” table
manufactured by Dell:
Aggregation Functions
Perform calculation on a set of values and return single value.
Syntax
Select Function(column) fromtable


                 Name             Salary

                Mohamed           4000

                 Hassan           3000

                 Doaa             6000

                 Ahmed            4000
Aggregation Functions

  Requirements                          Select Stmt                  Result

 Average of salary   Select AVG(salary) from employees;              4250

 Number of Rows      Select COUNT(*) from employees;                   4

 Distinct Salaries   Select COUNT(Distinct Salary) from employees;     3


   Highest Salary    Select MAX(salary) from employees;              6000
   Lowest Salary     Select MIN(salary) from employees;              3000
   Total Salaries    Select SUM(salary) from employees;              17000
Group By Clause


The GROUP BY statement is used in conjunction with
the aggregate functions to group the result-set by one
or more columns.
Group By Clause

Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
Example
Orders Table

         O_Id          Order_Date         Order_Price Customers
           1             2013/11/12            1000             Hansen
           2             2013/03/12            1600                 Nilsen
           3             2013/05/02            700              Hansen
           4             2013/07/09            300              Hansen
           5             2013/08/01            2000             Jensen
           6             2013/04/03            100                  Nilsen

Now we want to find the total sum (total order) of each customer.
Example
Sol
SELECT Customer,SUM(Order_Price) FROM Orders
GROUP BY Customer;

            Customer                   Sum(Order_Price)

 Hansen                        2000

 Nilsen                        1700

 Jinsen                        2000

PS: When using the same select stmt without Group By clause
it will fire an error.
Example
Ex: Sells (bar, beer, price)
Find average price for each peer.
Sol: Select Beer, AVG(Price)
       From Sells Group By Beer;

Ex: Get the name of the dept and its No. of Emp that
       make over 25,000$ per year.
Sol: Select department, COUNT(*) as 'Number of Employees'
       From Employees
       Where salary > 25000
       Group by department
Having Clause
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
• Also, HAVING is used in conjunction with the SELECT clause to
specify a search condition for a group. The HAVING clause behaves
like the WHERE clause, but is applicable to groups.
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
Ex: want to find if any of the customers have a total order of less than 2000.
Sol: SELECT Customer,SUM(OrderPrice)
    FROM Orders S
    GROUP BY Customer
    HAVING SUM(OrderPrice)<2000

Ex: want to find if the customers “Hansen” or “Jensen” have a total
order of more than 1500.
Sol: SELECT Customer,SUM(OrderPrice) FROM Orders
    WHERE Customer='Hansen' OR Customer='Jensen'
    GROUP BY Customer
    HAVING SUM(OrderPrice)>1500
Example
SELECT COUNT(*)
FROM EMPLOYEES GROUP BY ID
HAVING SALARY > 15000;
this query is illegal, because the column SALARY is not a grouping column, it
does not appear within an aggregate, and it is not within a subquery;

SELECT COUNT(*)
FROM EMPLOYEES GROUP BY ID
HAVING SUM(SALARY) > 15000;


Aggregates in the HAVING clause do not need to appear in the SELECT list
Diff. btw. Having & Where
1. HAVING specifies a search condition for a group or an aggregate
function used in SELECT statement. The WHERE clause specifies the
criteria which individual records must meet to be selected by a query. It
can be used without the GROUP BY clause. The HAVING clause
cannot be used without the GROUP BY clause.

2. The WHERE clause selects rows before grouping. The HAVING
clause selects rows after grouping.

3. The WHERE clause cannot contain aggregate functions. The
HAVING clause can contain aggregate functions.

More Related Content

What's hot

Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
Balqees Al.Mubarak
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
Syed Zaid Irshad
 
Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)
Mohd Tousif
 
Sql queires
Sql queiresSql queires
Sql queires
MohitKumar1985
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
Vineeta Garg
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handoutsjhe04
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Commands
CommandsCommands
Commands
Ayushi Goyal
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating dataecomputernotes
 
Sql powerpoint
Sql powerpointSql powerpoint
Sql powerpoint
Samuel Loch
 
SQL Course - QA
SQL Course - QASQL Course - QA
SQL Course - QApingkapil
 
Sql task
Sql taskSql task
Sql task
Nawaz Sk
 

What's hot (20)

Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
 
Les03
Les03Les03
Les03
 
06.02 sql alias
06.02 sql alias06.02 sql alias
06.02 sql alias
 
Sql wksht-1
Sql wksht-1Sql wksht-1
Sql wksht-1
 
Les02
Les02Les02
Les02
 
Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)
 
Sql queires
Sql queiresSql queires
Sql queires
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
Les08
Les08Les08
Les08
 
Module03
Module03Module03
Module03
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Commands
CommandsCommands
Commands
 
Les04
Les04Les04
Les04
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
Sql powerpoint
Sql powerpointSql powerpoint
Sql powerpoint
 
SQL Course - QA
SQL Course - QASQL Course - QA
SQL Course - QA
 
Sql task
Sql taskSql task
Sql task
 
Les07
Les07Les07
Les07
 

Similar to Dynamic websites lec2

75864 sql
75864 sql75864 sql
75864 sql
bansalaman80
 
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhhSQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
NaveeN547338
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
MrHello6
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
ANSHVAJPAI
 
SQL
SQLSQL
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
HAMEEDHUSSAINBU21CSE
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
ArjayBalberan1
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
Huda Alameen
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
BG Java EE Course
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
metriohanzel
 
Sql
SqlSql
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
Nitesh Singh
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
ADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptxADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptx
ArjayBalberan1
 
Chapter08
Chapter08Chapter08
Chapter08
sasa_eldoby
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
SabrinaShanta2
 

Similar to Dynamic websites lec2 (20)

75864 sql
75864 sql75864 sql
75864 sql
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhhSQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Clauses
ClausesClauses
Clauses
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
SQL
SQLSQL
SQL
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
 
Sql
SqlSql
Sql
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
ADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptxADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptx
 
Chapter08
Chapter08Chapter08
Chapter08
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 

Recently uploaded

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 

Dynamic websites lec2

  • 1. Dynamic Websites PHP with Oracle DB By Belal Arfa
  • 2. Section 2 In this section will discuss 1- Examples on last section 2- Quiz 3- Like Operator 4- Top Stmt 5- Union VS Union ALL 6- IN Operator 7- Aggregation Functions 8- Group By 9- Having 10- Difference Between Having and Where
  • 3. Quiz (1) • Relations: Movie(title, year, length, inColor, studioName, producerC#) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) • Queries: a) Find the address of MGM studios. b) Find Sandra Bullock’s birthdate. c) Find all the stars that appear either in a movie made in 1980 or a movie with “Love” in the title. d) Find all executives worth at least $10,000,000. e) Find all the stars who either are male or live in Miami ( have Miami as a part of their address).
  • 4. Quiz (1) Answer a) SELECT address FROM studio WHERE name = ‘MGM’; b) SELECT birthdate FROM moviestar WHERE name = ‘Sandra Bullock’; c) SELECT starName FROM StarsIn WHERE movieYear = 1980 OR movieTitle LIKE ‘%Love%’; d) SELECT name FROM MovieExec WHERE netWorth >= 10,000,000; e) SELECT name FROM MovieStar WHERE gender = ‘M’ OR address LIKE ‘% Miami %’;
  • 5. LIKE Operator • The LIKE operator is used to search for a specified pattern in a column.. • SQL LIKE Syntax: – SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern Persons Table P_ID LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger Ex. Want to select persons living in city ending with "s" from "Persons" table. (% can be used to define wildcards (missing letters in pattern)) Sol: SELECT * FROM Persons WHERE City LIKE ‘%s';
  • 6. SELECT TOP Stmt TOP clause is used to specify the number of records to return. Can be useful on large tables with ‘000s of records as Returning a large number of records can impact on performance. SQL TOP Syntax: SELECT column_name(s) FROM table_name WHERE ROWNUM <= number Ex : Want to select only the two first records in the table above Sol: SELECT * FROM Persons Where RowNum<2
  • 7. Union VS Union ALL The UNION operator returns only distinct rows that appear in either result, while the UNION ALL operator returns all rows. The UNION ALL operator does not eliminate duplicate selected rows.
  • 8. IN Operator Nested Queries – SELECT Model FROM Product WHERE ManufacturerID IN (SELECT ManufacturerID FROM Manufacturer WHERE Manufacturer = 'Dell') • The nested query above will select all models from the “Product” table manufactured by Dell:
  • 9. Aggregation Functions Perform calculation on a set of values and return single value. Syntax Select Function(column) fromtable Name Salary Mohamed 4000 Hassan 3000 Doaa 6000 Ahmed 4000
  • 10. Aggregation Functions Requirements Select Stmt Result Average of salary Select AVG(salary) from employees; 4250 Number of Rows Select COUNT(*) from employees; 4 Distinct Salaries Select COUNT(Distinct Salary) from employees; 3 Highest Salary Select MAX(salary) from employees; 6000 Lowest Salary Select MIN(salary) from employees; 3000 Total Salaries Select SUM(salary) from employees; 17000
  • 11. Group By Clause The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
  • 12. Group By Clause Syntax SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name
  • 13. Example Orders Table O_Id Order_Date Order_Price Customers 1 2013/11/12 1000 Hansen 2 2013/03/12 1600 Nilsen 3 2013/05/02 700 Hansen 4 2013/07/09 300 Hansen 5 2013/08/01 2000 Jensen 6 2013/04/03 100 Nilsen Now we want to find the total sum (total order) of each customer.
  • 14. Example Sol SELECT Customer,SUM(Order_Price) FROM Orders GROUP BY Customer; Customer Sum(Order_Price) Hansen 2000 Nilsen 1700 Jinsen 2000 PS: When using the same select stmt without Group By clause it will fire an error.
  • 15. Example Ex: Sells (bar, beer, price) Find average price for each peer. Sol: Select Beer, AVG(Price) From Sells Group By Beer; Ex: Get the name of the dept and its No. of Emp that make over 25,000$ per year. Sol: Select department, COUNT(*) as 'Number of Employees' From Employees Where salary > 25000 Group by department
  • 16. Having Clause The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. • Also, HAVING is used in conjunction with the SELECT clause to specify a search condition for a group. The HAVING clause behaves like the WHERE clause, but is applicable to groups. 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
  • 17. Example Ex: want to find if any of the customers have a total order of less than 2000. Sol: SELECT Customer,SUM(OrderPrice) FROM Orders S GROUP BY Customer HAVING SUM(OrderPrice)<2000 Ex: want to find if the customers “Hansen” or “Jensen” have a total order of more than 1500. Sol: SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500
  • 18. Example SELECT COUNT(*) FROM EMPLOYEES GROUP BY ID HAVING SALARY > 15000; this query is illegal, because the column SALARY is not a grouping column, it does not appear within an aggregate, and it is not within a subquery; SELECT COUNT(*) FROM EMPLOYEES GROUP BY ID HAVING SUM(SALARY) > 15000; Aggregates in the HAVING clause do not need to appear in the SELECT list
  • 19. Diff. btw. Having & Where 1. HAVING specifies a search condition for a group or an aggregate function used in SELECT statement. The WHERE clause specifies the criteria which individual records must meet to be selected by a query. It can be used without the GROUP BY clause. The HAVING clause cannot be used without the GROUP BY clause. 2. The WHERE clause selects rows before grouping. The HAVING clause selects rows after grouping. 3. The WHERE clause cannot contain aggregate functions. The HAVING clause can contain aggregate functions.