Database I. Practice SQL.

Slides:



Advertisements
Similar presentations
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Advertisements

Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Managing Schema Objects
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Nikolay Kostov Telerik Corporation
Manipulating Large Data Sets. Lesson Agenda ◦ Manipulating data using subqueries ◦ Specifying explicit default values in the INSERT and UPDATE statements.
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
Copyright © 2004, Oracle. All rights reserved. MANIPULATING LARGE DATA SETS Oracle Lecture 10.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
6 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
The Student Registry Database Ian Van Houdt and Muna alfahad.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
4 Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Using Data Model Editor to Create Data Models Based on a SQL Query Data Set.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
I Copyright © 2009, Oracle. All rights reserved. Introduction.
1 Copyright © 2006, Oracle. All rights reserved. Introduction.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Introduction.
Using Subqueries to Solve Queries
Using the Set Operators
Introduction.
ERD’s REVIEW DBS201.
Entity Relationship Diagrams ERDs
Restricting and Sorting Data
Introduction.
Using Subqueries to Solve Queries
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Introduction.
Using CASE Value expression
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

Database I. Practice SQL

Database Schema hr.job_history (employee_id, start_date, end_date, job_id, department_id) hr.jobs (job_id, job_title, min_salary, max_salary) hr.departments (department_id, department_name, manager_id, location_id) hr.employees (employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, comission_pct, manager_id, department_id) hr.locations (location_id, street_address, postal_code, city, state_province, country_id) hr.countries (country_id, country_name, region_id)

Exercises What is the telephone number and email address of Mozhe Atkinson? In which department does he work? (All attributes of the department in question should be returned.) How much does he earn more than the minimal salary that he could get for his job? What is the name of his manager? What are the names of those employees who work in a department whose name contains letters a and s in arbitrary order? What are the phone numbers of those employees who started their job after 15-03-2003? What are the email addresses of those emloyees who finished their job in March?

More Exercises… What are the names of those employees who work in Brazil and earn as much money as can be earned in that job they profess. What are the names of those employees who work in at least two different departments? What are the names of those departments that have at least two employees? What are those job titles that are professed in China but not in France? What are the name of those employees who worked more than 500 days in a department without interruption?

Subqueries What are the names of those departments that are located in Paris? (use IN) What are the names of those employees who have a colleague working at the same department and earning the same salary? (use EXISTS) What are the names of those departments, where everybody earns more than 1000 dollars? (use NOT IN) What is the name of the employee who has the highest salary? What are the names of those employees whose salary is greater than the lowest salary given for an employee? What are the names of those employees who earns more than at least two other employees? What is the name of that employee who has the second highest salary?

Aggregation How many employees work at the IT department? What are the maximum and minimum salaries that are paid to an employee? (The result should contain a single row with two columns.) Which job has the lowest minimum salary? What is the name of that department, where the employees earn the most all together? Who is that employee, who worked for the longest time in a department? Which is the longest email address?

Grouping For each department give the number of employees, who work in the department in question and the average of the salaries of these employees. Which is that city, where the most departments are located? Which is that job, where the sum of the salaries is the highest? Which are those jobs that are professed by at least five employees? What are the names of those departments in which even the lowest salary is higher than 2000?