0% found this document useful (0 votes)
383 views15 pages

Dbms Queries

This document summarizes a review session on SQL select statements and multi-relation queries. It covers select-from-where statements, exercises on 3-way joins and subqueries to find average salaries and compare companies. Sample queries are provided to find employees who live in the same city and street as their manager, earn more than their company's average salary, and earn more than employees at other companies.

Uploaded by

ganeshyahoo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
383 views15 pages

Dbms Queries

This document summarizes a review session on SQL select statements and multi-relation queries. It covers select-from-where statements, exercises on 3-way joins and subqueries to find average salaries and compare companies. Sample queries are provided to find employees who live in the same city and street as their manager, earn more than their company's average salary, and earn more than employees at other companies.

Uploaded by

ganeshyahoo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Review Session

Monday, Oct 8
Shipra Agrawal
Announcements
 New Gradiance assignment deadline
Wednesday, Oct 10
 Please read FAQs for assignments
Select-From-Where Statements
SELECT desired attributes
FROM one or more tables
WHERE condition about tuples of
the tables
Exercise
 Consider database

Product(name, price, category)

 Output
 each product name with its price in cents
 Also mark the products with price >10$ and
category=‘beverage’ as ‘expensive’ and
others as ‘reasonable’
Solution
 SELECT name, price*100, ‘expensive’
FROM Product
WHERE price >10 AND category =
‘beverage’
 UNION
 SELECT name, price*100, ‘reasonable’
FROM Product
WHERE price <=10 OR category
<>‘beverage’
Multirelation queries
 Consider database schema
employee( employee-name, street, city)
works( employee-name, company-name,
salary)
company( company-name, city)
manages( employee-name, manager-
name)
Exercise 1 (3-way join)
 Find all employees who live in the same cities as
the companies for which they work

SELECT employee-name
FROM employee, works
WHERE employee.employee-name =
works.employee-name AND works.company-
name = company.company-name AND
employee.city = company.city
Exercise 2
 Find all employees in the database who live on the same
city and streets as their manager.

 SELECT e1.employee-name
 FROM employee e1,employee e2, manages
 WHERE
e1.employee-name = manages.employee-name AND
e2. employee-name = manages.manager-name AND
e1.street = e2.street AND e1.city = e2.city
Exercise 3
 Find all employees who earn more than average salary
of all employees of their company

SELECT employee-name
FROM works w1,
(SELECT AVG(salary) AS avg-salary, company-name
FROM works
GROUP BY company-name)w2
WHERE w1.company-name = w2.company-name AND
w1.salary>w2.avg-salary
Exercise 4
 Find those companies whose employees earn a higher
salary, on average than the average salary at First Bank
Corporation.

SELECT company-name
FROM works
GROUP BY company-name
HAVING AVG(salary)>
(SELECT AVG(salary)
FROM works
GROUP BY company-name
HAVING company-name = ‘First Bank Corporation’)
Exercise 5
 Assume that the companies may be located in several cities. Find all the
companies located in every city in which Small Bank corporation is located.

SELECT company-name
FROM(
SELECT c1.company-name,c1.city
FROM company c1, company c2
WHERE c1.city=c2.city AND c2.company-name = ‘Small Bank
Corporation’
)R
GROUP BY company-name
HAVING COUNT(DISTINCT city) = (SELECT COUNT(DISTINCT city)
FROM company
GROUP BY company.company-name
HAVING company-name = ‘Small Bank
Corporation’)
Exercise 6
 Find all employees who earn more than each
employee of ‘Small Bank Corporation’
Select employee-name
From employee
Where employee-name NOT IN (
SELECT employee-name
FROM works w1,works w2
WHERE w1.salary < w2.salary AND w2.company-
name = ‘Small Bank Corporation’
)
Exercise 6 (contd..)
 Find all employees who earn more than each employee
of ‘Small Bank Corporation’
Select employee-name
From employee
Where name NOT IN (
SELECT employee-name
FROM works w1, (SELECT salary
FROM works
WHERE company-name=‘Small Bank
Corporation’) w2
WHERE w1.salary < w2.salary
)
Exercise 6
 Find all employees who earn more than each
employee of ‘Small Bank Corporation’
Select employee-name
From works
Where salary > (
SELECT MAX(salary)
FROM works
GROUP BY company-name
HAVING company-name = ‘Small Bank Corporation’
)
Questions?

You might also like