labact05d

Upload: azukadevil6667485

Post on 10-Jan-2016

220 views

Category:

Documents


0 download

DESCRIPTION

LabAct05d

TRANSCRIPT

  • Database Management Systems Laboratory Activity #05d

    During this activity, you should:

    learn more about the differences between Oracle and MySQL

    learn how top-N queries are implemented in Oracle

    learn how subqueries and set operators are implemented in Oracle

    develop further existing skills in using the SQL SELECT statement to extract data from a database

    I. Set Operators in Oracle Set operators (union, intersection, set difference) in MySQL are implemented via the UNION [ALL], IN (with subqueries) and NOT IN (with subqueries) operators. Although Oracle provides the same functionality and complies with the same guidelines on set operators (see Activity 4c), it provides direct implementations of intersection and set difference via the INTERSECT and MINUS operators.

    II. Views

    A view is a logical (or virtual) table based on one or more tables or views. A view contains no data itself. The

    SELECT statement associated with the view is executed when the view is opened. Queries can use not only physical tables but also views as data sources (tables found in the FROM clause).

    Views in MySQL:

    Q6. Create a view that determines the top 5 busiest actors (based on the number of films that they have appeared in). Indicate the name of the view and SQL statement used in the view.

    Heading of the view: actor_id, last_name, first_name, numfilms Required: first and last rows

    Q7. Using the view created above and other relevant tables in the sakila schema, provide the SQL statement that displays the films of these 5 actors.

    Heading: actor_id, last_name, first_name, title

    Result: 199 rows Required: 5th row

    Q1. Consider the UNION query below. How many rows does it return and what information does it

    provide? SELECT DISTINCT m.last_name, m.first_name FROM employees e JOIN employees m ON e.manager_id = m.employee_id UNION SELECT DISTINCT last_name, first_name FROM departments d JOIN employees m ON d.manager_id = m.employee_id;

    Q2. Provide the SQL statements that implements the corresponding intersection operation for the

    above query. Use the INTERSECT keyword. Indicate also the number of rows that are returned by the query.

    Q3. Provide the SQL statements that implements the corresponding set difference operations for the above query. Use the MINUS keyword. There should be two queries, since the set difference operation is not commutative. Indicate also the number of rows that are returned by each query.

    Q4. Provide the SQL statement that determines the salaries common to departments 30 and 50. Heading: Salary

    Result: 5 rows

    Q5. Provide the SQL statement that determines the departments that do not have employees who

    have salaries below 5000.

    Heading: Department_Name Result: 7 rows

  • Views in Oracle

    Q12. Create a view that provides information on all employees and their corresponding managers. All

    employees should be included in the results generated from the view.

    Heading of the view: EmpLast, EmpFirst, Salary, MgrLast, MgrFirst

    Result: 107 rows

    Required: Name of the view and the SQL statement used in the view

    Q13. Using the view in Q12, provide the SQL statement that determines the employee under King who

    gets the lowest salary.

    Heading: EmpLast, EmpFirst, Salary

    Required: resulting row

    Q8. Create a view displays all countries along with the languages spoken in these countries and the

    corresponding speaking population for each spoken language. The number of speakers should be

    expressed (rounded up) as whole numbers. Heading of the view: Country, Language, NumSpeakers

    Result: 984 rows Required: Name of the view and the SQL statement used in the view

    Q9. Using the view above, provide the SQL statement that displays the total number of speakers for a

    given language. The list should be sorted by the total number of speakers (in descending order). Heading: Language Total Number of Speakers

    Result: 457 Required: In what row is English found and what is its total speaking population?

    Q10. Using the view in Q8 as the data source, create another view that determines, for each country,

    its largest speaking population for a spoken language. Include only aggregate values that are greater than 0 (use the HAVING clause).

    Heading of the view: Country, NumSpeakers

    Result: 213 rows Required: Name of the view and the SQL statement used in the view

    Q11. Using the two views created in Q8 and Q10, provide the SQL statement (use the appropriate join

    operation) that will display the top spoken languages for each country, along with its speaking

    population.

    Heading of the view: Country, Language, NumSpeakers Result: 213 rows; 1st row - Afghanistan, Pashto, 11905280

    Required: 10th row