Structured Query Language (SQL) Advanced Features

Size: px
Start display at page:

Download "Structured Query Language (SQL) Advanced Features"

Transcription

1 Explicit Joins Structured Query Language (SQL) Advanced Features Joins are usually specified as part of the WHERE statement (implicit or innerjoins.) Further, LEFT, RIGHT, and FULL OUTER joins can also be specified. Joins can also be specified as part of the FROM clause explicitly (theta join) computed tables Instructor: Sharma Chakravarthy The University of Texas at Arlington Database Management Systems: Sharma Chakravarthy 1 SELECT select-list FROM table_1 {LEFT RIGHT FULL} [OUTER] JOIN table_2 ON join_condition_1 [{LEFT RIGHT FULL} [OUTER] JOIN table_3 ON join_condition_2] CROSS JOIN can also be used for Cartesian product INNER JOIN can also be used Database Management Systems: Sharma Chakravarthy 2 Example Union 26. Find the numbers of the players who are older than R. Parmenter SELECT FROM WHERE A.playerno PLAYERS as A JOIN PLAYERS as P on A.yob < P.yob P.name = 'Parmenter' AND P.initials = 'R' You can write Queries in the way that suits you! The optimizer will optimize it in the best possible way! Syntax SELECT_statement_1 UNION [ALL] SELECT_statement_2 [UNION [ALL] SELECT_statement_3] [ORDER BY order_by_list] Database Management Systems: Sharma Chakravarthy 3 Database Management Systems: Sharma Chakravarthy 4 1

2 Aggregate Functions AVG([ALL DISTINCT] expression) The average of the non-null values in the expression SUM([ALL DISTINCT] expression) The total of the non-null values in the expression MIN([ALL DISTINCT] expression) The lowest non-null values in the expression MAX([ALL DISTINCT] expression) The highest non-null values in the expression COUNT([ALL DISTINCT] expression) The number of non-null values in the expression COUNT(*) //can also use DISTINCT and attr names The number of rows selected by a query Aggregate Functions Aggregate functions (or column functions) perform calculation on the values in a set of selected rows (partition) Expression specified for AVG, SUM and COUNT functions must result in a numeric value. The expression for Min and MAX can result in a numeric, date, or string value By default, all values are included in the calculation regardless of whether they are duplicates. Use DISTINCT keyword to omit duplicates If you use an aggregate function in the SELECT clause, that clause cannot include non-aggregate columns from the base table (except group by attributes) Database Management Systems: Sharma Chakravarthy 5 Database Management Systems: Sharma Chakravarthy 6 Aggregate Functions Can a HAVING clause have aggregate functions? Yes! Can a SELECT clause have aggregate functions? Yes! Can a WHERE clause have aggregate functions? No! Can a WHERE clause have subselects in it? Yes! Can a HAVING clause have subselects in it? Yes! Aggregate Functions PLAYERS (playerno*, name, initials, yob, sex, year_joined, street, houseno, postcode, town, phoneno, leagueno); GAMES (teamno*, playerno*, won, lost); 27. List names of players who have won more games than they have lost (for all the teams they have played) select name from players p where (select sum(won) from games g1 where g1.playerno = p.playerno) > (select sum(lost) from games g2 where g2.playerno = p.playerno); Can a WHERE clause have subselects that have aggregates in them? Yes, need to make sure it is properly used! Database Management Systems: Sharma Chakravarthy 7 NAME Parmenter Baker Collins Moorman Brown Database Management Systems: Sharma Chakravarthy 8 2

3 Aggregate Functions 28. list names of players who have won more games than they have lost when they played for a team select name from players p where (select won from games g1 where g1.playerno = p.playerno) > (select lost from games g2 where g2.playerno = p.playerno); Gives error: single-row subquery returns more than one row! Tables Instances SQL> select * from penalties; PAYMENTNO PLAYERNO PDATE AMOUNT rows selected. Database Management Systems: Sharma Chakravarthy 9 Database Management Systems: Sharma Chakravarthy 10 Aggregate Functions PLAYERS (playerno*, name, initials, yob, sex, year_joined, street, houseno, postcode, town, phoneno, leagueno); PENALTIES (paymentno*, playerno, date, amount); 29. list names of players and their max penalty select playerno, (select max(amount) from penalties pe where pe.playerno=pl.playerno) as maxpmt from players pl order by maxpmt desc; PLAYERNO MAXPMT Database Management Systems: Sharma Chakravarthy Aggregate Functions A HAVING clause can refer to a column included in the select clause whereas a WHERE clause can refer to any columns in the base tables Can use AND and OR in the HAVING clause Can use comparisons using relational arithmetic operators Group by name HAVING sum(bonus) > sum(salary) Can use simple predicates in the HAVING clause, but aggregates cannot be used in the WHERE clause A SELECT can only include columns in the group by clause (+ aggregate functions)! Database Management Systems: Sharma Chakravarthy 12 3

4 Aggregate Functions (SQL Extensions) Several RDBMSs support OLAP (on-line analytical processing) used in data warehouses as opposed to OLTP (online transaction processing) used in traditional DBMSs ROLLUP Summary row is added for each group and the total CUBE Summary row is added for each dimension (or distinct combination of the group) and the total TOP n [PERCENT] output top n tuples or top n% tuples Overview What s data Analysis Overview of Tools for analysis in relational model Problems with GROUP BY and why SQL need to be fixed Need for ROLL UP and CUBE operators Top n [PERCENT] [WITH TIES] if the last row has ties (same values), all are presented Database Management Systems: Sharma Chakravarthy 13 11/8/2017 Sharma Chakravarthy 14 Requirements Data analysis - Summarize data value - Look for unusual pattern of data - Extract statistical information - Contrast one category with another Steps in data analysis - Extracting the aggregated data from the database into a file or table - Visualizing the result in graphical way, display trends, clusters and difference Questions? How do traditional relational database fit into this picture? How can relations (FACT table) represent multidimensional problem? How to use existing tools in SQL and enrich it further 11/8/2017 Sharma Chakravarthy 15 11/8/2017 Sharma Chakravarthy 16 4

5 A typical STAR Schema Time Altitude Fact Latitude Longitude Time Altitude Temp Pres Latitude... Longitude Multi-dimensional data Relational systems model N- dimensional data as a relation with N-attributes Weather Time Latitude Longitude Altitude Temp Pres 27/ 11/ 94: : 58: 33N 122: 45: 28W / 11/ 94: : 16: 18N 27: 05: 55W /8/2017 Sharma Chakravarthy 17 11/8/2017 Sharma Chakravarthy 18 Relational Tools Additional SQL functions SQL aggregate function: COUNT(), SUM(), MIN(), MAX(), and AVG() Example: The average of all measured temperatures is: SELECT AVG( Temp) FROM Weather; The following query counts the distinct number of reporting time SELECT COUNT( DISTINCT Time) FROM Weather 11/8/2017 Sharma Chakravarthy 19 Statistic: median, standard deviation, variance, etc. Physical: center of mass, angular momentum, etc. Financial analysis: volatility, alpha, beta, etc. Some system allow users to add new aggregation function 11/8/2017 Sharma Chakravarthy 20 5

6 Group BY Figure 1: The GROUP BY relational operator partitions a table into groups. Each group is then aggregated by a function. The aggregation function summarizes some column of groups returning a value for each group. Grouping Values Partitioned Table Sum() Aggregate Values 11/8/2017 Sharma Chakravarthy 21 Group-By Used to create a table of many aggregate values indexed by a set of attributes Weather Time Latitude Longitude Altitude Temp Pres 27/ 11/ 94: : 58: 33N 122: 45: 28W / 11/ 94: : 16: 18N 27: 05: 55W / 11/ 94: : 58: 33S 150: 10: 10W / 11/ 94: : 58: 33S 150: 10: 10W SELECT Time, Altitude, AVG( Temp) FROM Weather GROUP BY Time, Altitude; Weather Time Altitude Avg Temp 27/ 11/ 94: / 11/ 94: One may want to group by time, altitude, and both To do this, you have to write 3 or more queries and take union Sharma Chakravarthy 22 An Example SALES Model Year Color Sales Chevy 1990 red 5 Chevy 1990 white 87 Chevy 1990 blue 62 Chevy 1991 red 54 Chevy 1991 white 95 Chevy 1991 blue 49 Chevy 1992 red 31 Chevy 1992 white 54 Chevy 1992 blue 71 Ford 1990 red 64 Ford 1990 white 62 Ford 1990 blue 63 Ford 1991 red 52 Ford 1991 white 9 Ford 1991 blue 55 Ford 1992 red 27 Ford 1992 white 62 Ford 1992 blue 39 Roll-Up and Drill-Down Sales Roll Up by Model by Year by Color Sales by Model Sales by Sales Model Year Color by Year by Color Model by Year by Model Chevy 1994 black 50 white black 85 white Problem -not relational model (null value in the primary key not allowed) -not convenient, the number of columns grows as the power set of the number of aggregated attributes 11/8/2017 Sharma Chakravarthy 24 6

7 Roll-Up and Drill-Down A more convenient representation in relational model Sales Summary Model Year Color Units Chevy 1994 black 50 Chevy 1994 white 40 Chevy 1994 ALL 90 Chevy 1995 black 85 Chevy 1995 white 115 Chevy 1995 ALL 200 Chevy ALL ALL /8/2017 Sharma Chakravarthy 25 SQL Approach SQL statements to build the SalesSummary table from the raw sales data SELECT Model, ALL, ALL, SUM( Sales) FROM Sales WHERE Model = Chevy GROUP BY Model UNION SELECT Model, Year, ALL, SUM( Sales) FROM Sales WHERE Model = Chevy GROUP BY Model, Year UNION SELECT Model, Year, Color, SUM( Sales) FROM Sales WHERE Model = Chevy GROUP BY Model, Year, Color 11/8/2017 Sharma Chakravarthy 26 SQL Approach SQL Approach SELECT ALL, ALL, ALL, SUM( Sales) FROM Sales UNION SELECT Model, ALL, ALL, SUM( Sales) FROM Sales GROUP BY Model UNION SELECT ALL, Year, ALL, SUM( Sales) FROM Sales GROUP BY Year UNION SELECT ALL, ALL, Color, SUM( Sales) FROM Sales GROUP BY Color UNION SELECT Model, Year, ALL, SUM( Sales) FROM Sales GROUP BY Model, Year UNION SELECT Model, ALL, Color, SUM( Sales) FROM Sales GROUP BY Model, Color UNION SELECT ALL, Year, Color, SUM( Sales) FROM Sales GROUP BY Year, Color UNION SELECT Model, Year, Color, SUM( Sales) FROM Sales GROUP BY Model, Year, Color 11/8/2017 Sharma Chakravarthy 27 Notice: Roll up to one direction Still complicated Does not represent all aspects of roll up and drill down To extend from this example, for full roll- up and drill- down in 3D Needs 8 aggregate SQL statements Problem: complicated SQL query for 4D, 16 aggregates are needed 11/8/2017 Sharma Chakravarthy 28 7

8 N-dimensional Cube where Each Attribute is a Dimension N-dimensional Aggregate (sum(), max(),...) fits relational model exactly: a 1, a 2,..., a N, f() Super-aggregate over N-1 Dimensional sub-cubes ALL, a 2,..., a N, f() a 3, ALL, a 3,..., a N, f()... a 1, a 2,..., ALL, f() this is the N-1 Dimensional cross-tab. Super-aggregate over N-2 Dimensional sub-cubes ALL, ALL, a 3,..., a N, f()... a 1, a 2,..., ALL, ALL, f() SALES Model Year Color Sales Chevy 1990 red 5 Chevy 1990 white 87 Chevy 1990 blue 62 Chevy 1991 red 54 Chevy 1991 white 95 Chevy 1991 blue 49 Chevy 1992 red 31 Chevy 1992 white 54 Chevy 1992 blue 71 Ford 1990 red 64 Ford 1990 white 62 Ford 1990 blue 63 Ford 1991 red 52 Ford 1991 white 9 Ford 1991 blue 55 Ford 1992 red 27 Ford 1992 white 62 Ford 1992 blue 39 An Example CUBE DATA CUBE Model Year Color Sales ALL ALL ALL 942 chevy ALL ALL 510 ford ALL ALL 432 ALL 1990 ALL 343 ALL 1991 ALL 314 ALL 1992 ALL 285 ALL ALL red 165 ALL ALL white 273 ALL ALL blue 339 chevy 1990 ALL 154 chevy 1991 ALL 199 chevy 1992 ALL 157 ford 1990 ALL 189 ford 1991 ALL 116 ford 1992 ALL 128 chevy ALL red 91 chevy ALL white 236 chevy ALL blue 183 ford ALL red 144 ford ALL white 133 ford ALL blue 156 ALL 1990 red 69 ALL 1990 white 149 ALL 1990 blue 125 ALL 1991 red 107 ALL 1991 white 104 ALL 1991 blue 104 ALL 1992 red 59 ALL 1992 white 116 ALL 1992 blue 110 The Data CUBE Operator To simplify the computation mentioned above Use the ROLL UP and CUBE operator GROUP BY [ WITH ( CUBE ROLL UP) ] { ( <column name> <expression> ) } 11/8/2017 Sharma Chakravarthy 31 CUBE Operator If the application wants only a roll-up or drilldown report, the full cube is an overkill It is reasonable to offer other additional rollup functions in addition to CUBE. ROLLUP produces just the super-aggregates: (f1, f2,, ALL),. (f1, ALL,, ALL) (ALL, ALL,, ALL) 11/8/2017 Sharma Chakravarthy 32 8

9 Aggregate Functions (SQL Extensions) Several RDBMSs support OLAP (on-line analytical processing) used in data warehouses as opposed to OLTP (online transaction processing) used in traditional DBMSs ROLLUP Summary row is added for each group and the total CUBE Summary row is added for each dimension (or distinct combination of the group) and the total TOP n [PERCENT] output top n tuples or top n% tuples Top n [PERCENT] [WITH TIES] if the last row has ties (same values), all are presented Database Management Systems: Sharma Chakravarthy 33 ROLLUP INVOICES(invid, vid, invnum, invdate, description, total); Select vid, count(*) as invcount, sum(total) as invtotal From invoices Group by ROLLUP (vid); //multiple attributes are ok; // Oracle syntax Vid invcount invtotal Syntax varies from vendor to vendor GROUP BY vid with ROLLUP (for SQL server) You can also have order by Database Management Systems: Sharma Chakravarthy 34 Summary row ROLLUP VENDORS(venid, name, address, venstate, vencity); Select venstate, vencity, count(*) as qtyven, From vendors Group by ROLLUP (venstate, vencity); Order by venstate DESC, vencity DESC Venstate vencity qtyven NJ washington 1 NJ fairfield 1 NJ east brunswick 2 NJ NULL 4 IA washington 1 IA fairfield 1 IA NULL 2 NULL NULL 6 Summary row Summary rows ROLLUP select playerno, sum(amount) from penalties group by rollup (playerno); PLAYERNO SUM(AMOUNT) Summary row Database Management Systems: Sharma Chakravarthy 35 Database Management Systems: Sharma Chakravarthy 36 9

10 CUBE Select venstate, vencity, count(*) as qtyven, From vendors Where venstate in { IA, NJ ) Group by CUBE (venstate, vencity) Order by venstate DESC, vencity DESC Venstate vencity qtyven NJ washington 1 NJ fairfield 1 NJ east brunswick 2 NJ NULL 4 Summary row IA washington 1 IA fairfield 1 IA NULL 2 Summary row NULL washington 2 NULL fairfield 2 NULL east brunswick 2 NULL NULL 6 Summary rows Database Management Systems: Sharma Chakravarthy 37 CUBE PLAYERNO PDATE SUM(AMOUNT) select playerno, pdate, sum(amount) 480 from penalties group by cube (playerno, pdate); Summary row Summary row rows selected. Database Management Systems: Sharma Chakravarthy 38 CUBE Operator Comments on the CUBE Operator it aggregates over all <select list> as in standard group by then it unions in each super aggregate of the global cube substitute ALL for the aggregation column If the cardinality of the N attributes are C 1, C 2, C 3,., C n. The result will be (C i + 1) Previous Example: (1+1)* (2+1) = 6 rows Sales Example: (2+1)*(3+1)*(3+1) = 48 rows (18 in sales + 30 in data cube) The extra value is ALL Summary CUBE operator generalizes relational aggregates Needs ALL value to denote sub-cubes ALL values represent aggregation sets Needs generalization of user-defined aggregates Decorations and abstractions are interesting Computation has interesting optimizations Relationship to rest of SQL not fully worked out. 11/8/2017 Sharma Chakravarthy 39 10

11 Histograms Histograms (Contd.) In Standard SQL, GROUP BY operator does NOT allow a direct construction of Histograms (aggregation over computed categories) Ex: SELECT day, nation, MAX( Temp) FROM Weather GROUP BY Day( time) as day, Country( Latitude, Longitude) as nation; 11/8/2017 Sharma Chakravarthy 41 Some SQL system do support histogram (SQL92) SELECT day, nation, MAX( Temp) FROM (SELECT Day( Time) as day, Country( Latitude, Longitude) as nation, Temp FROM Weather ) AS foo GROUP BY day, nation; Not convenient 11/8/2017 Must construct a table-valued Sharma Chakravarthy expression and 42 then aggregate over resulting table Joins Vs. subselects Advantage of implicit/explicit joins The result of a join operation can include columns from both tables. Only outer table columns can be included when using a subquery A query with an implicit join typically performs faster than the same with a subquery Advantages of subqueries You can pass an aggregate value to the outer query A subquery tends to be more intuitive Long, complex queries can be easier to code using subqueries subqueries A subquery is a SELECT statement that is used within another SQL statement A subquery can return a single value, a result set that contains a single column, or a result set that contains one or more columns A subquery that returns a single value can be used anywhere an expression is allowed. A subquery that returns a single column can be used in place of a list of values A subquery that returns one or more columns can be used in place of a table in the FROM clause Some DBMSs allow multiple attributes to be compared Database Management Systems: Sharma Chakravarthy 43 Database Management Systems: Sharma Chakravarthy 44 11

12 Subqueries Subqueries can be used in 4 ways: In a WHERE clause as part of a condition (as long as it returns a single value) In a HAVING clause as part of a condition (as long as it returns a single value for a partition) In a FROM clause as a table specification //table expression In a SELECT clause as a column specification Database Management Systems: Sharma Chakravarthy 45 Subqueries Select distinct vendorname, (select MAX(invoicedate) form Invoices where Invoices.vendorID = Vendors.vendorID) as LatestInv from Vendors order by LatestInv DESC Alternatively, Select distinct vendorname, MAX(invoicedate) Form Vendors join Invoices on Invoices.vendorID = Vendors.vendorID) as LatestInv Group by VendorName order by LatestInv DESC Database Management Systems: Sharma Chakravarthy 46 Subqueries Alternatively, Select distinct vendorname, MAX(invoicedate) as latestinv Form Vendors, Invoices Where Invoices.vendorID = Vendors.vendorID Group by VendorName order by LatestInv DESC Subselect Subquery in a SELECT clause used for a column specification must return a single value Typically a correlated subquery Can be rewritten using a join instead of the subquery. Join is faster and more readable and hence subqueries are seldom used in a select statement Database Management Systems: Sharma Chakravarthy 47 Database Management Systems: Sharma Chakravarthy 48 12

13 Subselect in a FROM Clause Nested Queries SELECT Invoices.VendorID, MAX(InvoiceDate) FROM Invoices JOIN (SELECT TOP 5 VendorID, AVG(invoiceTotal) as AvgInvoice FROM Invoices GROUP BY VendorID ORDER BY AvgInvoice DESC) AS TopVendor ON Invoices.VendorID=TopVendor.VendorID Group By Invoices.VendorID Order BY LatestInv DESC Classification single-shot evaluation of sub/nested queries Example : Select employees whose salary is the same as the average salary Select From Where Name Employee Salary = (Select AVG (salary) From Employee) The subquery returns a single value. Hence the predicate can be modified as Salary = K where k is the result of the sub query Database Management Systems: Sharma Chakravarthy 49 Database Management Systems: Sharma Chakravarthy 50 Nested Queries (Contd.) Nested Queries (Contd.) Sub/nested query can return a set Example: select employees whose department is located in Denver Select Name From Employee Where Dept_number IN (Select Dept_Number From Dept Where location= Denver ) Modify the predicate appropriately at run time (as an IN clause which has an explicit set as part of the query Database Management Systems: Sharma Chakravarthy 51 The previous query can be re-written without nesting Select Name From Employee e, Dept d Where e.dept_number = d.dept_number and d.location= Denver The optimizer may not do (or most likely not able to do) this transformation Database Management Systems: Sharma Chakravarthy 52 13

14 Nested Queries (Contd.) Nested Queries (Contd.) Select names of Employees who earn more than their managers This query cannot be written like earlier queries with a subquery that can be evaluated once (or is independent) In principle, a correlation query must be re-evaluated for each candidate tuple from the referenced query block Correlated sub query a sub query that contains reference to a value obtained from a candidate tuple of a higher level query block Example Select Name From Employee e where Salary > (Select Salary From Employee where Employee.Num=e.Manager) The subquery has to be evaluated for each tuple of the outer query (nested loop query) Database Management Systems: Sharma Chakravarthy 53 Database Management Systems: Sharma Chakravarthy 54 Nested Queries (Contd.) Nested Queries (Contd.) Correlated sub query a sub query that contains reference to a value obtained from a candidate tuple of a higher level query block Example Select Name From Employee Where Dept_number IN (Select Dept_Number From Dept Where location= Denver ) The above query is not correlated The previous query (employee s salary greater than his/her manager) can be written without using subqueries (as a non-correlated query) Example Select From where Name Employee e, Employee m e.salary > m.salary AND e.num=m.manager Database Management Systems: Sharma Chakravarthy 55 Database Management Systems: Sharma Chakravarthy 56 14

15 Multi level correlated subqueries Select names of employee s who earn more than their manager s manager. L1 select name from Employee e where salary > L2 (select salary from Employee where Employee.ssn = L3 (select mgrssn From Employee where Employee.ssn = e.mgrssn)) Correlated subqueries Some correlated subqueries CANNOT be expressed in a simpler manner (as non-correlated subqueries) Example: Division queries Since L3 query references a L1 value, L3 need to be evaluated for each L1 tuple. Database Management Systems: Sharma Chakravarthy 57 Database Management Systems: Sharma Chakravarthy 58 Nested Queries Nested block is optimized independently, with the outer tuple considered as providing a selection condition. Outer block is optimized with the cost of `calling nested block computation taken into account. Implicit ordering of these blocks means that some good strategies are not considered. The non-nested version of the query is typically optimized better. SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) Nested block to optimize: SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid= outer value Equivalent non-nested query: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid Database Management Systems: Sharma Chakravarthy 59 DIVISION SUPPLIER (s#, sname, status, city) PARTS (p#, pname, color, weight, city) SP (s#, p#, qty) Get supplier names for suppliers who supply ALL parts (SP[s#, p#] PARTS[p#]) join SUPPLIER on s# There is NO division operator or (logical) for all quantifier in SQL θ [any all] is not adequate to express division! How do we translate the above query into SQL? AND R.bid=103 Database Management Systems: Sharma Chakravarthy 60 15

16 DIVISION SUPPLIER (s#, sname, status, city) PARTS (p#, pname, color, weight, city) SP (s#, p#, qty) DIVISION SUPPLIER (s#, sname, status, city) PARTS (p#, pname, color, weight, city) SP (s#, p#, qty) It is based on the equivalence of the logical expression x ( y (P(x, y)) ( x ( y (P(x, y))) The following 2 statements are equivalent: 1. For all x (parts) there exists a y (supplier) such that P(x, y) is true (y supplies all x (parts)) 2. It is not the case (false) that there exists a x (part) for which no y (supplier) exists such that P(x, y) is true (y supplies part x) Database Management Systems: Sharma Chakravarthy 61 Get supplier names for suppliers who supply ALL parts parts ( supplier (supplies(supplier, parts)) ( part ( supplier (supplies(supplier, parts))) The following 2 statements are equivalent: 1. For all parts p there exists a supplier s such that supplies(s, p) is true 2. It is not the case (false) that there exists a part for which no supplier exists such that suplies(s, p) is true Database Management Systems: Sharma Chakravarthy 62 DIVISION SUPPLIER (s#, sname, status, city) PARTS (p#, pname, color, weight, city) SP (s#, p#, qty) SELECT sname FROM supplier s /*needed for output */ WHERE NOT EXISTS ( SELECT parts FROM parts p /*for all denominator*/ minus ( SELECT parts FROM SP sp /* there exists numerator*/ WHERE sp.s# = s.s# ) ) Database Management Systems: Sharma Chakravarthy 63 DIVISION SUPPLIER (s#, sname, status, city) PARTS (p#, pname, color, weight, city) SP (s#, p#, qty) SELECT DISTINCT sname //is distinct needed? FROM supplier s //needed for output WHERE NOT EXISTS (SELECT * FROM parts p //for all denominator WHERE NOT EXISTS (SELECT * FROM SP sp //there exists numerator WHERE sp.s# = s.s# AND sp.p# = p.p#) ) Database Management Systems: Sharma Chakravarthy 64 16

17 DIVISION SUPPLIER (s#, sname, status, city) PARTS (p#, pname, color, weight, city) SP (s#, p#, qty) Get suppliers who supply ALL red parts Other operators SELECT FROM WHERE DISTINCT sname supplier s NOT EXISTS (SELECT * FROM parts p //all variable comes here WHERE p.color = red AND NOT EXISTS (SELECT * FROM SP sp //there exists var WHERE sp.s# = s.s# AND sp.p# = p.p#) INTERSECT EXCEPT MINUS Creation of views Views are not stored Tables are stored Database ) Management Systems: Sharma Chakravarthy 65 Database Management Systems: Sharma Chakravarthy 66 Other Features Grant and revoke Create views Rollback and commit The catalog is maintained as a table Operations on meta data is done using the same language! Limitations of SQL SQL is not like other programming languages SQL is non-procedural and is not imperative You do not write algorithms, but express you intent of what you want to retrieve The System (or the DBMS query optimizer) actually generates the algorithm and code for each SQL query submitted. This is not easy or straight forward SQL is not Turing complete. In other words, it cannot be used in lieu of a programming language Cannot compute median or other customized computations Database Management Systems: Sharma Chakravarthy 67 Database Management Systems: Sharma Chakravarthy 68 17

18 Embedded SQL As the name implies, SQL is embedded into a programming language Provides features that are not supported in SQL directly (if-then-else, looping, ) Can write canned packages to accept parameters from users Combines the capability of SQL with that of a programming language Arbitrary computations can be performed on data retrieved and stored in tables Embedded SQL Main(){. exec sql begin declare section; declare variables exec sql end declare section; exec sql include sqlca; exec sql select cname, discnt into :cust_name, :cust_discnt from customers where cid= :cusr_id } A single row is extracted in the above! What if it is a set? Database Management Systems: Sharma Chakravarthy 69 Database Management Systems: Sharma Chakravarthy 70 Embedded SQL Cursors Declare a cursor with a name Open a cursor Fetch tuples one at a time into program variables Close the cursor Cursors can be used for read or update; need to be declared; Scrollable cursors are used to move back and forth with in a scroll window of tuples Compiled by the pre-processor Compiled and linked by the language processor Executed! Median and mode can be computed! Transitive closure can be computed! Database Management Systems: Sharma Chakravarthy 71 Dynamic SQL So far, we discussed static SQL. That is program variables held values but NOT SQL statements! In other words, the embedded SQL statements cannot change at run time! SQL statements cannot be generated or parameterized at run time based on user input! The above is needed for writing interfaces to accept inputs and generate appropriate SQL statements based on the input! Dynamic SQL solves this problem! You need to use this for phase 5 of the project Database Management Systems: Sharma Chakravarthy 72 18

19 Dynamic SQL Dynamic SQL (Contd.) Execute immediate Exec sql execute immediate :host_variable host_variable contains an sql statement as a string Example: char sqltext[] = delete from players where playerno=24 ; exec sql execute immediate :sqltext; Execute immediate The statement is compiled once The statement cannot change If you want to change it, needs recompilation of the code. How do you overcome the above? Provide a mechanism to compile dynamically Provide a mechanism to execute dynamically Database Management Systems: Sharma Chakravarthy 73 Database Management Systems: Sharma Chakravarthy 74 Prepare, execute, and using prepare Exec sql prepare delete_playerno from :host_variable Exec sql execute delete_playerno using :var1, :var2,, :vark; Example: char sqltext[] = delete from players where playerno= :pno ; exec sql prepare delete_playerno from :sqltext; Parses and generates compiled form which is parameterized exec sql execute delete_playerno using :pno; Executes the compiled form using the variable Prepare and execure In the previous example, there were NO values returned If the sql stmt has a select, there is a problem we cannot include arbitrary number of attributes to be returned In order to overcome the above problem, a descriptor area has been introduced Describe statement needs to be used Cursor is used to extract values iteratively Database Management Systems: Sharma Chakravarthy 75 Database Management Systems: Sharma Chakravarthy 76 19

20 PL/SQL, TransactSQL In addition to embedding sql in various programming languages, each vendor has a language in which programming language features have been included PL/SQL is Oracle s procedural extension of SQL TransactSQL is Sybase s procedural extension of sql. Seamless integration of sql and other language constructs Can compile and store PL/SQL code and can be invoked Procedures, Triggers and user-defined functions A stored procedure is one or more sql statements that have been compiled and stored with the database Stored procedures are compiled and optimized the first time they are executed. In contrast, SQL statements that are sent from the client to the server have to be compiled and optimized every time they are executed In addition to select statements, it can contain other sql statements. It can also contain control-flow language for conditional processing A trigger is a special type of procedure executed automatically when a table is modified. Triggers are typically used to check the validity of data in a row that is being updated. A user-defined-function (UDF) is a special type of procedure that can return a value or a table Database Management Systems: Sharma Chakravarthy 77 Database Management Systems: Sharma Chakravarthy 78 Procedures, Triggers and user-defined functions The three types of procedural programs stored procedure, triggers and user-define-functions are executable database objects. These are stored with in the database. Stored procedures are frequently written by SQL programmers for use by end users or application programmers. You can provide access to the database exclusively thru stored procedures. Both user-defined-function (UDF) and Triggers are used more often by sql programmers than application programmers or end users. UDF is a special type of procedure that can return a value or a table They differ in whether or not they can use parameters. Both stored procedures and user-defined-functions can use parameters, but triggers cannot. Triggers Create trigger trigger_name On {table_name view_name} {for after instead of} [insert] [,] [update] [,] [delete] As sql_statements With in a trigger, you can refer to two tables: inserted or deleted Syntax may be slightly different for each vendor; need to look it up and use it correctly! Database Management Systems: Sharma Chakravarthy 79 Database Management Systems: Sharma Chakravarthy 80 20

21 SQL Summary Although not Turing-complete, SQL is a powerful non-procedural language for relational data management SQL has been optimized extensively over 30 years SQL as a standard evolves every 4 to 5 years and a number of additional features are added to accommodate growing needs Triggers, abstract data types, object-oriented features have been added in addition to OLAP operators (cube, rollup etc.), XML, temporal features etc. etc. It will continue to evolve! Now NoSQL (not only SQL) is also becoming mainstream!! Database Management Systems: Sharma Chakravarthy 81 SQL Summary (Contd.) An English query can be translated into more than one SQL equivalent. Some may be easier to understand (by humans). Some are better optimized by the underlying system Mining and warehousing has brought a number of limitations of relational query optimizers They were developed assuming not more than 8 to 10 joins Today, SQL queries with 50+ joins are common when data mining is done on a database Tuners or wizards are being developed to simplify (or replace) the job of an administrator Database Management Systems: Sharma Chakravarthy 82 Thank You! 21

DATA CUBE : A RELATIONAL AGGREGATION OPERATOR GENERALIZING GROUP-BY, CROSS-TAB AND SUB-TOTALS SNEHA REDDY BEZAWADA CMPT 843

DATA CUBE : A RELATIONAL AGGREGATION OPERATOR GENERALIZING GROUP-BY, CROSS-TAB AND SUB-TOTALS SNEHA REDDY BEZAWADA CMPT 843 DATA CUBE : A RELATIONAL AGGREGATION OPERATOR GENERALIZING GROUP-BY, CROSS-TAB AND SUB-TOTALS SNEHA REDDY BEZAWADA CMPT 843 WHAT IS A DATA CUBE? The Data Cube or Cube operator produces N-dimensional answers

More information

System R Optimization (contd.)

System R Optimization (contd.) System R Optimization (contd.) Instructor: Sharma Chakravarthy sharma@cse.uta.edu The University of Texas @ Arlington Database Management Systems, S. Chakravarthy 1 Optimization Criteria number of page

More information

5. Single-row function

5. Single-row function 1. 2. Introduction Oracle 11g Oracle 11g Application Server Oracle database Relational and Object Relational Database Management system Oracle internet platform System Development Life cycle 3. Writing

More information

SQL Workshop. Subqueries. Doug Shook

SQL Workshop. Subqueries. Doug Shook SQL Workshop Subqueries Doug Shook Subqueries A SELECT statement within a SELECT statement Four ways to code one: WHERE (*) HAVING (*) FROM SELECT Syntax is the same Rare to see GROUP BY or HAVING 2 Subqueries

More information

SQL STRUCTURED QUERY LANGUAGE

SQL STRUCTURED QUERY LANGUAGE STRUCTURED QUERY LANGUAGE SQL Structured Query Language 4.1 Introduction Originally, SQL was called SEQUEL (for Structured English QUery Language) and implemented at IBM Research as the interface for an

More information

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems, R. Ramakrishnan and J. Gehrke 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in our

More information

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL Objectives In this chapter, you will learn: How to use the advanced SQL JOIN operator syntax About the different

More information

Acknowledgment. MTAT Data Mining. Week 7: Online Analytical Processing and Data Warehouses. Typical Data Analysis Process.

Acknowledgment. MTAT Data Mining. Week 7: Online Analytical Processing and Data Warehouses. Typical Data Analysis Process. MTAT.03.183 Data Mining Week 7: Online Analytical Processing and Data Warehouses Marlon Dumas marlon.dumas ät ut. ee Acknowledgment This slide deck is a mashup of the following publicly available slide

More information

Chapter 7 How to code subqueries

Chapter 7 How to code subqueries Chapter 7 How to code subqueries Murach's MySQL, C7 2015, Mike Murach & Associates, Inc. Slide 1 Objectives Applied Code SELECT statements that require subqueries. Knowledge Describe the way subqueries

More information

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept] Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept] 1. What is DBMS? A Database Management System (DBMS) is a program that controls creation, maintenance and use

More information

SQL. Chapter 5 FROM WHERE

SQL. Chapter 5 FROM WHERE SQL Chapter 5 Instructor: Vladimir Zadorozhny vladimir@sis.pitt.edu Information Science Program School of Information Sciences, University of Pittsburgh 1 Basic SQL Query SELECT FROM WHERE [DISTINCT] target-list

More information

SQL: Queries, Constraints, Triggers

SQL: Queries, Constraints, Triggers SQL: Queries, Constraints, Triggers [R&G] Chapter 5 CS4320 1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for the Reserves relation contained

More information

SQL Interview Questions

SQL Interview Questions SQL Interview Questions SQL stands for Structured Query Language. It is used as a programming language for querying Relational Database Management Systems. In this tutorial, we shall go through the basic

More information

CIS 330: Applied Database Systems

CIS 330: Applied Database Systems 1 CIS 330: Applied Database Systems Lecture 7: SQL Johannes Gehrke johannes@cs.cornell.edu http://www.cs.cornell.edu/johannes Logistics Office hours role call: Mondays, 3-4pm Tuesdays, 4:30-5:30 Wednesdays,

More information

Oracle Syllabus Course code-r10605 SQL

Oracle Syllabus Course code-r10605 SQL Oracle Syllabus Course code-r10605 SQL Writing Basic SQL SELECT Statements Basic SELECT Statement Selecting All Columns Selecting Specific Columns Writing SQL Statements Column Heading Defaults Arithmetic

More information

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy CompSci 516 Data Intensive Computing Systems Lecture 3 SQL - 2 Instructor: Sudeepa Roy Announcements HW1 reminder: Due on 09/21 (Thurs), 11:55 pm, no late days Project proposal reminder: Due on 09/20 (Wed),

More information

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems CompSci 516 Database Systems Lecture 3 More SQL Instructor: Sudeepa Roy Duke CS, Fall 2018 CompSci 516: Database Systems 1 Announcements HW1 is published on Sakai: Resources -> HW -> HW1 folder Due on

More information

20461: Querying Microsoft SQL Server 2014 Databases

20461: Querying Microsoft SQL Server 2014 Databases Course Outline 20461: Querying Microsoft SQL Server 2014 Databases Module 1: Introduction to Microsoft SQL Server 2014 This module introduces the SQL Server platform and major tools. It discusses editions,

More information

SQL. SQL DDL Statements

SQL. SQL DDL Statements SQL Structured Query Language Declarative Specify the properties that should hold in the result, not how to obtain the result Complex queries have procedural elements International Standard SQL1 (1986)

More information

SQL: Queries, Programming, Triggers

SQL: Queries, Programming, Triggers SQL: Queries, Programming, Triggers CSC343 Introduction to Databases - A. Vaisman 1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for the

More information

Oracle PL SQL Training & Certification

Oracle PL SQL Training & Certification About Intellipaat Intellipaat is a fast-growing professional training provider that is offering training in over 150 most sought-after tools and technologies. We have a learner base of 600,000 in over

More information

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 6 Professional Program: Data Administration and Management MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) AGENDA

More information

Basic form of SQL Queries

Basic form of SQL Queries SQL - 1 Week 6 Basic form of SQL Queries SELECT FROM WHERE target-list relation-list qualification target-list A list of attributes of output relations in relation-list relation-list A list of relation

More information

More SQL: Complex Queries, Triggers, Views, and Schema Modification

More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries

More information

Enterprise Database Systems

Enterprise Database Systems Enterprise Database Systems Technological Educational Institution of Larissa in collaboration with Staffordshire University Larissa 2006 Dr. Georgia Garani garani@teilar.gr Dr. Theodoros Mitakos teo_ms@yahoo.com

More information

Oracle Database 11g: SQL and PL/SQL Fundamentals

Oracle Database 11g: SQL and PL/SQL Fundamentals Oracle University Contact Us: +33 (0) 1 57 60 20 81 Oracle Database 11g: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn In this course, students learn the fundamentals of SQL and PL/SQL

More information

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select, Sub queries A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select, Result of the inner query is passed to the main

More information

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6 SQL Part 2 Kathleen Durant PhD Northeastern University CS3200 Lesson 6 1 Outline for today More of the SELECT command Review of the SET operations Aggregator functions GROUP BY functionality JOIN construct

More information

More SQL: Complex Queries, Triggers, Views, and Schema Modification

More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries

More information

12. MS Access Tables, Relationships, and Queries

12. MS Access Tables, Relationships, and Queries 12. MS Access Tables, Relationships, and Queries 12.1 Creating Tables and Relationships Suppose we want to build a database to hold the information for computers (also refer to parts in the text) and suppliers

More information

AVANTUS TRAINING PTE LTD

AVANTUS TRAINING PTE LTD [MS20461]: Querying Microsoft SQL Server 2014 Length : 5 Days Audience(s) : IT Professionals Level : 300 Technology : SQL Server Delivery Method : Instructor-led (Classroom) Course Overview This 5-day

More information

Sql Server Syllabus. Overview

Sql Server Syllabus. Overview Sql Server Syllabus Overview This SQL Server training teaches developers all the Transact-SQL skills they need to create database objects like Tables, Views, Stored procedures & Functions and triggers

More information

Introduction to Computer Science and Business

Introduction to Computer Science and Business Introduction to Computer Science and Business This is the second portion of the Database Design and Programming with SQL course. In this portion, students implement their database design by creating a

More information

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment Introductory concepts of DBMS 1. Explain detailed 3-level architecture

More information

COURSE OUTLINE: Querying Microsoft SQL Server

COURSE OUTLINE: Querying Microsoft SQL Server Course Name 20461 Querying Microsoft SQL Server Course Duration 5 Days Course Structure Instructor-Led (Classroom) Course Overview This 5-day instructor led course provides students with the technical

More information

Querying Microsoft SQL Server

Querying Microsoft SQL Server Querying Microsoft SQL Server Course 20461D 5 Days Instructor-led, Hands-on Course Description This 5-day instructor led course is designed for customers who are interested in learning SQL Server 2012,

More information

UNIT-IV (Relational Database Language, PL/SQL)

UNIT-IV (Relational Database Language, PL/SQL) UNIT-IV (Relational Database Language, PL/SQL) Section-A (2 Marks) Important questions 1. Define (i) Primary Key (ii) Foreign Key (iii) unique key. (i)primary key:a primary key can consist of one or more

More information

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data 1 Writing Basic SQL SELECT Statements Objectives 1-2 Capabilities of SQL SELECT Statements 1-3 Basic SELECT Statement 1-4 Selecting All Columns 1-5 Selecting Specific Columns 1-6 Writing SQL Statements

More information

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals NEW Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training delivers the fundamentals of SQL and PL/SQL along with the

More information

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls Today s topics CompSci 516 Data Intensive Computing Systems Lecture 4 Relational Algebra and Relational Calculus Instructor: Sudeepa Roy Finish NULLs and Views in SQL from Lecture 3 Relational Algebra

More information

Slides by: Ms. Shree Jaswal

Slides by: Ms. Shree Jaswal Slides by: Ms. Shree Jaswal Overview of SQL, Data Definition Commands, Set operations, aggregate function, null values, Data Manipulation commands, Data Control commands, Views in SQL, Complex Retrieval

More information

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems CompSci 516 Data Intensive Computing Systems Lecture 2 SQL Instructor: Sudeepa Roy Duke CS, Spring 2016 CompSci 516: Data Intensive Computing Systems 1 Announcement If you are enrolled to the class, but

More information

Oracle Database 10g: Introduction to SQL

Oracle Database 10g: Introduction to SQL ORACLE UNIVERSITY CONTACT US: 00 9714 390 9000 Oracle Database 10g: Introduction to SQL Duration: 5 Days What you will learn This course offers students an introduction to Oracle Database 10g database

More information

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led About this course This course is designed to introduce students to Transact-SQL. It is designed in such a way that the first three days

More information

More SQL: Complex Queries, Triggers, Views, and Schema Modification

More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries

More information

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Oracle Database: SQL and PL/SQL Fundamentals Ed 2 Oracle University Contact Us: Local: 1800 103 4775 Intl: +91 80 67863102 Oracle Database: SQL and PL/SQL Fundamentals Ed 2 Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals

More information

DB2 SQL Class Outline

DB2 SQL Class Outline DB2 SQL Class Outline The Basics of SQL Introduction Finding Your Current Schema Setting Your Default SCHEMA SELECT * (All Columns) in a Table SELECT Specific Columns in a Table Commas in the Front or

More information

Querying Data with Transact-SQL

Querying Data with Transact-SQL Querying Data with Transact-SQL 20761B; 5 Days; Instructor-led Course Description This course is designed to introduce students to Transact-SQL. It is designed in such a way that the first three days can

More information

Oracle SQL & PL SQL Course

Oracle SQL & PL SQL Course Oracle SQL & PL SQL Course Complete Practical & Real-time Training Job Support Complete Practical Real-Time Scenarios Resume Preparation Lab Access Training Highlights Placement Support Support Certification

More information

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 6 Basic SQL Slide 6-2 Chapter 6 Outline SQL Data Definition and Data Types Specifying Constraints in SQL Basic Retrieval Queries in SQL INSERT, DELETE, and UPDATE Statements in SQL Additional Features

More information

Querying Microsoft SQL Server 2008/2012

Querying Microsoft SQL Server 2008/2012 Querying Microsoft SQL Server 2008/2012 Course 10774A 5 Days Instructor-led, Hands-on Introduction This 5-day instructor led course provides students with the technical skills required to write basic Transact-SQL

More information

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL SQL Join Operators Join operation merges rows from two tables and returns the rows with one of the following:

More information

20461D: Querying Microsoft SQL Server

20461D: Querying Microsoft SQL Server 20461D: Querying Microsoft SQL Server Course Details Course Code: Duration: Notes: 20461D 5 days This course syllabus should be used to determine whether the course is appropriate for the students, based

More information

SQL - Lecture 3 (Aggregation, etc.)

SQL - Lecture 3 (Aggregation, etc.) SQL - Lecture 3 (Aggregation, etc.) INFS 614 INFS614 1 Example Instances S1 S2 R1 sid bid day 22 101 10/10/96 58 103 11/12/96 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid

More information

II. Structured Query Language (SQL)

II. Structured Query Language (SQL) II. Structured Query Language () Lecture Topics Basic concepts and operations of the relational model. The relational algebra. The query language. CS448/648 1 Basic Relational Concepts Relating to descriptions

More information

Querying Data with Transact-SQL

Querying Data with Transact-SQL Querying Data with Transact-SQL Course 20761C 5 Days Instructor-led, Hands on Course Information The main purpose of the course is to give students a good understanding of the Transact- SQL language which

More information

Database Management Systems,

Database Management Systems, Database Management Systems SQL Query Language (3) 1 Topics Aggregate Functions in Queries count sum max min avg Group by queries Set Operations in SQL Queries Views 2 Aggregate Functions Tables are collections

More information

Querying Microsoft SQL Server

Querying Microsoft SQL Server Course Code: M20461 Vendor: Microsoft Course Overview Duration: 5 RRP: POA Querying Microsoft SQL Server Overview This 5-day instructor led course provides delegates with the technical skills required

More information

Querying Microsoft SQL Server

Querying Microsoft SQL Server Querying Microsoft SQL Server 20461D; 5 days, Instructor-led Course Description This 5-day instructor led course provides students with the technical skills required to write basic Transact SQL queries

More information

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course: Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course: 20762C Developing SQL 2016 Databases Module 1: An Introduction to Database Development Introduction to the

More information

CSEN 501 CSEN501 - Databases I

CSEN 501 CSEN501 - Databases I CSEN501 - Databases I Lecture 5: Structured Query Language (SQL) Prof. Dr. Slim slim.abdennadher@guc.edu.eg German University Cairo, Faculty of Media Engineering and Technology Structured Query Language:

More information

SQL functions fit into two broad categories: Data definition language Data manipulation language

SQL functions fit into two broad categories: Data definition language Data manipulation language Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition Chapter 7 Beginning Structured Query Language (SQL) MDM NUR RAZIA BINTI MOHD SURADI 019-3932846 razia@unisel.edu.my

More information

20761B: QUERYING DATA WITH TRANSACT-SQL

20761B: QUERYING DATA WITH TRANSACT-SQL ABOUT THIS COURSE This 5 day course is designed to introduce students to Transact-SQL. It is designed in such a way that the first three days can be taught as a course to students requiring the knowledge

More information

Querying Microsoft SQL Server 2012/2014

Querying Microsoft SQL Server 2012/2014 Page 1 of 14 Overview This 5-day instructor led course provides students with the technical skills required to write basic Transact-SQL queries for Microsoft SQL Server 2014. This course is the foundation

More information

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls? L05: SQL 183 Announcements! HW1 is due tonight HW2 groups are assigned Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls? 184 Small IMDB schema (SQLite)

More information

20761 Querying Data with Transact SQL

20761 Querying Data with Transact SQL Course Overview The main purpose of this course is to give students a good understanding of the Transact-SQL language which is used by all SQL Server-related disciplines; namely, Database Administration,

More information

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql ASSIGNMENT NO. 3 Title: Design at least 10 SQL queries for suitable database application using SQL DML statements: Insert, Select, Update, Delete with operators, functions, and set operator. Requirements:

More information

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions... Contents Contents...283 Introduction...283 Basic Steps in Query Processing...284 Introduction...285 Transformation of Relational Expressions...287 Equivalence Rules...289 Transformation Example: Pushing

More information

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 7 More SQL: Complex Queries, Triggers, Views, and Schema Modification Slide 7-2 Chapter 7 Outline More Complex SQL Retrieval Queries Specifying Semantic Constraints as Assertions and Actions as

More information

II. Structured Query Language (SQL)

II. Structured Query Language (SQL) II. Structured Query Language (SQL) Lecture Topics ffl Basic concepts and operations of the relational model. ffl The relational algebra. ffl The SQL query language. CS448/648 II - 1 Basic Relational Concepts

More information

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Charting the Course to Your Success! MOC D Querying Microsoft SQL Server Course Summary Course Summary Description This 5-day instructor led course provides students with the technical skills required to write basic Transact-SQL queries for Microsoft SQL Server 2014. This course is the foundation

More information

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama The Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4113: Database Lab Lab # 6 Using Subqueries and Set Operators Eng. Alaa O Shama November, 2015 Objectives:

More information

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Tenth Edition Chapter 7 Introduction to Structured Query Language (SQL) Objectives In this chapter, students will learn: The basic commands and

More information

CS6302 DBMS 2MARK & 16 MARK UNIT II SQL & QUERY ORTIMIZATION 1. Define Aggregate Functions in SQL? Aggregate function are functions that take a collection of values as input and return a single value.

More information

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018 Introduction to SQL ECE 650 Systems Programming & Engineering Duke University, Spring 2018 SQL Structured Query Language Major reason for commercial success of relational DBs Became a standard for relational

More information

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 Outline More Complex SQL Retrieval

More information

Querying Data with Transact SQL

Querying Data with Transact SQL Course 20761A: Querying Data with Transact SQL Course details Course Outline Module 1: Introduction to Microsoft SQL Server 2016 This module introduces SQL Server, the versions of SQL Server, including

More information

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries.

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries. Handout 9 CS-605 Spring 18 Page 1 of 8 Handout 9 SQL Select -- Multi Table Queries. Joins and Nested Subqueries. Joins In Oracle https://docs.oracle.com/cd/b19306_01/server.102/b14200/queries006.htm Many

More information

Querying Microsoft SQL Server

Querying Microsoft SQL Server 20461 - Querying Microsoft SQL Server Duration: 5 Days Course Price: $2,975 Software Assurance Eligible Course Description About this course This 5-day instructor led course provides students with the

More information

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17 Announcement CompSci 516 Database Systems Lecture 10 Query Evaluation and Join Algorithms Project proposal pdf due on sakai by 5 pm, tomorrow, Thursday 09/27 One per group by any member Instructor: Sudeepa

More information

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances COSC 304 Introduction to Database Systems SQL Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca SQL Queries Querying with SQL is performed using a SELECT statement. The general

More information

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions. Dr.G.R.Damodaran College of Science (Autonomous, affiliated to the Bharathiar University, recognized by the UGC)Re-accredited at the 'A' Grade Level by the NAAC and ISO 9001:2008 Certified CRISL rated

More information

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time SQL Basics & PL-SQL Complete Practical & Real-time Training Sessions A Unit of SequelGate Innovative Technologies Pvt. Ltd. ISO Certified Training Institute Microsoft Certified Partner Training Highlights

More information

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led CENTER OF KNOWLEDGE, PATH TO SUCCESS Website: QUERYING MICROSOFT SQL SERVER Course: 20461C; Duration: 5 Days; Instructor-led WHAT YOU WILL LEARN This 5-day instructor led course provides students with

More information

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

CIS 330: Applied Database Systems. ER to Relational Relational Algebra CIS 330: Applied Database Systems ER to Relational Relational Algebra 1 Logical DB Design: ER to Relational Entity sets to tables: ssn name Employees lot CREATE TABLE Employees (ssn CHAR(11), name CHAR(20),

More information

Querying Data with Transact-SQL

Querying Data with Transact-SQL Querying Data with Transact-SQL Duration: 5 Days Course Code: M20761 Overview: This course is designed to introduce students to Transact-SQL. It is designed in such a way that the first three days can

More information

More on SQL Nested Queries Aggregate operators and Nulls

More on SQL Nested Queries Aggregate operators and Nulls Today s Lecture More on SQL Nested Queries Aggregate operators and Nulls Winter 2003 R ecom m en ded R eadi n g s Chapter 5 Section 5.4-5.6 http://philip.greenspun.com/sql/ Simple queries, more complex

More information

20761C: Querying Data with Transact-SQL

20761C: Querying Data with Transact-SQL 20761C: Querying Data with Transact-SQL Course Details Course Code: Duration: Notes: 20761C 5 days This course syllabus should be used to determine whether the course is appropriate for the students, based

More information

CMP-3440 Database Systems

CMP-3440 Database Systems CMP-3440 Database Systems Relational DB Languages Relational Algebra, Calculus, SQL Lecture 05 zain 1 Introduction Relational algebra & relational calculus are formal languages associated with the relational

More information

SQL: The Query Language Part 1. Relational Query Languages

SQL: The Query Language Part 1. Relational Query Languages SQL: The Query Language Part 1 CS 186, Fall 2002, Lecture 9 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump) Relational Query Languages A major strength of the relational model:

More information

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014 COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014 MODULE 1: INTRODUCTION TO MICROSOFT SQL SERVER 2014 This module introduces the SQL Server platform and major tools. It discusses editions, versions,

More information

CHAPTER 5 SECURITY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

CHAPTER 5 SECURITY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI CHAPTER 5 SECURITY ADVANCED DATABASE SYSTEMS Assist. Prof. Dr. Volkan TUNALI Topics 2 Introduction Discretionary Access Control Mandatory Access Control Statistical Databases Data Encryption SQL Facilities

More information

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2 CMPT 354 Simple queries Set operations Aggregate operators Null values Joins Query Optimization John Edgar 2 Data Manipulation Language (DML) to Write queries Insert, delete and modify records Data Definition

More information

SQL Data Query Language

SQL Data Query Language SQL Data Query Language André Restivo 1 / 68 Index Introduction Selecting Data Choosing Columns Filtering Rows Set Operators Joining Tables Aggregating Data Sorting Rows Limiting Data Text Operators Nested

More information

Keys, SQL, and Views CMPSCI 645

Keys, SQL, and Views CMPSCI 645 Keys, SQL, and Views CMPSCI 645 SQL Overview SQL Preliminaries Integrity constraints Query capabilities SELECT-FROM- WHERE blocks, Basic features, ordering, duplicates Set ops (union, intersect, except)

More information

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity COSC 416 NoSQL Databases Relational Model (Review) Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Relational Model History The relational model was proposed by E. F. Codd

More information

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1 NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E) 2 LECTURE OUTLINE More Complex SQL Retrieval Queries Self-Joins Renaming Attributes and Results Grouping, Aggregation, and Group Filtering

More information

1 Dulcian, Inc., 2001 All rights reserved. Oracle9i Data Warehouse Review. Agenda

1 Dulcian, Inc., 2001 All rights reserved. Oracle9i Data Warehouse Review. Agenda Agenda Oracle9i Warehouse Review Dulcian, Inc. Oracle9i Server OLAP Server Analytical SQL Mining ETL Infrastructure 9i Warehouse Builder Oracle 9i Server Overview E-Business Intelligence Platform 9i Server:

More information

20461: Querying Microsoft SQL Server

20461: Querying Microsoft SQL Server 20461: Querying Microsoft SQL Server Length: 5 days Audience: IT Professionals Level: 300 OVERVIEW This 5 day instructor led course provides students with the technical skills required to write basic Transact

More information

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1 SQL Fundamentals Chapter 3 Class 03: SQL Fundamentals 1 Class 03: SQL Fundamentals 2 SQL SQL (Structured Query Language): A language that is used in relational databases to build and query tables. Earlier

More information