Top Banner
Q2-1 CUBE generates a result set that represents aggregates for all combinations of values in the selected columns. ROLLUP generates a result set that represents aggregates for a hierarchy of values in the selected columns. The ROLLUP, CUBE, and GROUPING SETS operators are extensions of the GROUP BY clause. The ROLLUP, CUBE, or GROUPING SETS operators can generate the same result set as when you use UNION ALL to combine single grouping queries; however, using one of the GROUP BY operators is usually more efficient. The GROUPING SETS operator can generate the same result set as that generated by using a simple GROUP BY, ROLLUP, or CUBE operator. When all the groupings that are generated by using a full ROLLUP or CUBE operator are not required, you can use GROUPING SETS to specify only the groupings that you want. The GROUPING SETS list can contain duplicate groupings; and, when GROUPING SETS is used with ROLLUP and CUBE, it might generate duplicate groupings. Duplicate groupings are retained as they would be by using UNION ALL. Q2-2 You can selectively specify the set of groups that you want to create using a GROUPING SETS expression within a GROUP BY clause. This allows precise specification across multiple dimensions without computing the whole CUBE . GROUPING SET is a subset of CUBE. Q2-3 A composite column is a collection of columns that are treated as a unit during the computation of groupings. Q2-4 MySQL has a nice aggregate function called GROUP_CONCAT , which concatenates all strings in a group in given order, separating them with a given separator.
15

Cs457k HW#8 12538 Manoj Kandepu

Dec 25, 2015

Download

Documents

ManojKandepu

db hw
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cs457k HW#8 12538 Manoj Kandepu

Q2-1

CUBE generates a result set that represents aggregates for all combinations of values in the selected columns.

ROLLUP generates a result set that represents aggregates for a hierarchy of values in the selected columns.

The ROLLUP, CUBE, and GROUPING SETS operators are extensions of the GROUP BY clause. The ROLLUP, CUBE, or GROUPING SETS operators can generate the same result set as when you use UNION ALL to combine single grouping queries; however, using one of the GROUP BY operators is usually more efficient.The GROUPING SETS operator can generate the same result set as that generated by using a simple GROUP BY, ROLLUP, or CUBE operator. When all the groupings that are generated by using a full ROLLUP or CUBE operator are not required, you can use GROUPING SETS to specify only the groupings that you want. The GROUPING SETS list can contain duplicate groupings; and, when GROUPING SETS is used with ROLLUP and CUBE, it might generate duplicate groupings. Duplicate groupings are retained as they would be by using UNION ALL.

Q2-2

You can selectively specify the set of groups that you want to create using a GROUPING SETS expression within a GROUP BY clause. This allows precise specification across multiple dimensions without computing the whole CUBE.

GROUPING SET is a subset of CUBE.

Q2-3

A composite column is a collection of columns that are treated as a unit during the computation of groupings. 

Q2-4

MySQL has a nice aggregate function called GROUP_CONCAT, which concatenates all strings in a group in given order, separating them with a given separator.

Q2-5

A subquery is best defined as a query within a query. Subqueries enable you to write queries that select data rows for criteria that are actually developed while the query is executing at run time. More formally, it is the use of a SELECT statement inside one of the clauses of another SELECT statement. n fact, a subquery can be contained inside another subquery, which is inside another subquery, and so forth. A subquery can also be nested inside INSERT, UPDATE, and DELETE statements. Subqueries must be enclosed within parentheses.

Page 2: Cs457k HW#8 12538 Manoj Kandepu

Q2-6

Column comparisons in a multiple-column subquery can be pairwise comparisons or nonpairwise

comparisons.A multi-Column Subquery is simply a subquery that returns more than one column.

Q2-7

A scalar subquery expression is a subquery that returns exactly one column value from one row. The value of the scalar subquery expression is the value of the select list item of the subquery. If the subquery returns 0 rows, then the value of the scalar subquery expression is NULL. If the subquery returns more than one row, then Oracle returns an error.

Q2-8

A correlated subquery is a subquery that contains a reference to a table that also appears in the outer query. For example:

SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2 WHERE t2.column2 = t1.column2);

Q2-9

EXISTS is a Comparison operator, which is used to check and match records between two queries on correlation basis and returns a BOOLEAN output (TRUE or FALSE). The two queries are designated as the Outer or Parent query and the Sub query.

Note: NOT EXISTS is the negation format of EXISTS.

Q2-10

In Oracle PL/SQL, the WITH clause is a subquery factoring clause which is used to create a named subquery block. This block acts as a virtual table or an inline view for a SQL statement. It was first introduced in Oracle 9.2. It reduces the overhead of multiple references of a table alias in a query. The scope of the WITH clause subquery block is the SELECT query with which is associated.

Page 3: Cs457k HW#8 12538 Manoj Kandepu

1. Write a SQL statement that display by department_id, total of salary for each department & total of employee for each department from employee table? (5 Points)

SELECT DEPARTMENT_ID, SUM (SALARY), COUNT (EMPLOYEE_ID) FROM EMPLOYEE GROUP BY DEPARTMENT_ID;

2) Write a SQL statement that cumulative aggregates display total salary for each department and also total salary for whole company? (5 Points)

SELECT DEPARTMENT_ID, JOB_ID, SUM (SALARY) FROM EMPLOYEE GROUP BY ROLLUP (DEPARTMENT_ID, JOB_ID);

Page 4: Cs457k HW#8 12538 Manoj Kandepu

3. Write a SQL statement that display cross-tabulation values by department_id, job_id and total salary for each department and job? (5 Points)SELECT DEPARTMENT_ID, JOB_ID, SUM (SALARY) FROM EMPLOYEE GROUP BY CUBE (DEPARTMENT_ID, JOB_ID);

Page 5: Cs457k HW#8 12538 Manoj Kandepu

4. Write a SQL statement that use grouping function for department_id and job_id to cumulative aggregates display by department_id and job_id and summary of salary (5 Points)

SELECT DEPARTMENT_ID DEPTID, JOB_ID JOB, SUM (SALARY),

GROUPING (DEPARTMENT_ID) GRP_DEPT, GROUPING (JOB_ID) GRP_JOB FROM EMPLOYEE GROUP BY ROLLUP (DEPARTMENT_ID,

JOB_ID);

Page 6: Cs457k HW#8 12538 Manoj Kandepu
Page 7: Cs457k HW#8 12538 Manoj Kandepu

5. Write a SQL statement that displays first name and last name and their salary is greater than employee ‘Joden’?  (5 Points)

SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEE WHERE SALARY>(SELECT SALARY FROM EMPLOYEE WHERE FIRST_NAME=’JODREN’);

Page 8: Cs457k HW#8 12538 Manoj Kandepu

6. Write a SQL statement that shows who earn more than the average salary in their company? (5 Points)

SELECT FIRST_NAME ||' '|| LAST_NAME EMPNAME, JOB_ID, SALARY FROM EMPLOYEE WHERE SALARY>(SELECT AVG (SALARY) FROM EMPLOYEE);

Page 9: Cs457k HW#8 12538 Manoj Kandepu
Page 10: Cs457k HW#8 12538 Manoj Kandepu

7. Write a SQL statement that using the WITH clause to display the department name and total salaries for those departments, whose total salary is greater than the average salary across departments? (5 Points)

with 2 dept_costs as(select department_name,sum(salary) as dept_total from employee,department where employee.department_id=department.department_id 3 group by department_name),avg_cost as (select sum(dept_total)/count(*) as dept_avg from dept_costs) 4 select * from dept_costs where dept_total > (select dept_avg from avg_cost) order by department_name;

Page 11: Cs457k HW#8 12538 Manoj Kandepu

8. Write a SQL statement that using NOT EXIST operator to display all departments that do not have any employees? (5 Points)

SELECT DEPARTMENT_ID, DEPARTMENT_NAME FROM DEPARTMENT D WHERE NOT EXISTS (SELECT * FROM EMPLOYEE WHERE DEPARTMENT_ID=D.DEPARTMENT_ID);

Page 12: Cs457k HW#8 12538 Manoj Kandepu

9. Complete task as (10 Points)        a. Add a DEPARTMENT_NAME VARCHAR2 (20) column into EMPLOYEE table         b. Update the column value accordingly base on department table        c. Use SQL*Plus commend to show table structure        d. Query EMPLOYEE table to display the result        e. Drop the column

Page 13: Cs457k HW#8 12538 Manoj Kandepu

A. ALTER TABLE EMPLOYEE ADD (DEPARTMENT_NAME VARCHAR2 (20));B. UPDATE EMPLOYEE E SET DEPARTMENT_NAME = (SELECT

DEPARTMENT_NAME FROM DEPARTMENT D WHERE E.DDEPARTMENT_ID = D.DEPARTMENT_ID);

C. DESC EMPLOYEE;D. SELECT * FROM EMPLOYEE;E. ALTER TABLE EMPLOYEE DROP COLUMN DEPARTMENT_NAME;