Upcoming SlideShare
×

# Dynamic websites lec2

477 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
477
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
11
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Dynamic websites lec2

1. 1. Dynamic WebsitesPHP with Oracle DB By Belal Arfa
2. 2. Section 2In this section will discuss1- Examples on last section2- Quiz3- Like Operator4- Top Stmt5- Union VS Union ALL6- IN Operator7- Aggregation Functions8- Group By9- Having10- Difference Between Having and Where
3. 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 moviewith “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 apart of their address).
4. 4. Quiz (1) Answera) 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. 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 patternPersons Table P_ID LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 StavangerEx. 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. 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 alargenumber of records can impact on performance. SQL TOP Syntax:SELECT column_name(s)FROM table_nameWHERE ROWNUM <= numberEx : Want to select only the two first records in the table aboveSol: SELECT * FROM Persons Where RowNum<2
7. 7. Union VS Union ALLThe UNION operator returns only distinct rowsthat appear in either result,while the UNION ALL operator returns all rows.The UNION ALL operator does not eliminateduplicate selected rows.
8. 8. IN Operator Nested Queries– SELECT Model FROM ProductWHERE ManufacturerID IN(SELECT ManufacturerID FROM ManufacturerWHERE Manufacturer = Dell)• The nested query above will select all models from the “Product” tablemanufactured by Dell:
9. 9. Aggregation FunctionsPerform calculation on a set of values and return single value.SyntaxSelect Function(column) fromtable Name Salary Mohamed 4000 Hassan 3000 Doaa 6000 Ahmed 4000
10. 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. 11. Group By ClauseThe GROUP BY statement is used in conjunction withthe aggregate functions to group the result-set by oneor more columns.
12. 12. Group By ClauseSyntaxSELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name
13. 13. ExampleOrders 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 NilsenNow we want to find the total sum (total order) of each customer.
14. 14. ExampleSolSELECT Customer,SUM(Order_Price) FROM OrdersGROUP BY Customer; Customer Sum(Order_Price) Hansen 2000 Nilsen 1700 Jinsen 2000PS: When using the same select stmt without Group By clauseit will fire an error.
15. 15. ExampleEx: 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. 16. Having ClauseThe HAVING clause was added to SQL because the WHERE keywordcould not be used with aggregate functions.• Also, HAVING is used in conjunction with the SELECT clause tospecify a search condition for a group. The HAVING clause behaveslike the WHERE clause, but is applicable to groups.Syntax:SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value
17. 17. ExampleEx: 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)<2000Ex: want to find if the customers “Hansen” or “Jensen” have a totalorder 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. 18. ExampleSELECT COUNT(*)FROM EMPLOYEES GROUP BY IDHAVING SALARY > 15000;this query is illegal, because the column SALARY is not a grouping column, itdoes not appear within an aggregate, and it is not within a subquery;SELECT COUNT(*)FROM EMPLOYEES GROUP BY IDHAVING SUM(SALARY) > 15000;Aggregates in the HAVING clause do not need to appear in the SELECT list
19. 19. Diff. btw. Having & Where1. HAVING specifies a search condition for a group or an aggregatefunction used in SELECT statement. The WHERE clause specifies thecriteria which individual records must meet to be selected by a query. Itcan be used without the GROUP BY clause. The HAVING clausecannot be used without the GROUP BY clause.2. The WHERE clause selects rows before grouping. The HAVINGclause selects rows after grouping.3. The WHERE clause cannot contain aggregate functions. TheHAVING clause can contain aggregate functions.