Question

Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in...

Consider the following database schema:
LIKE(person, sport),
PRACTICE(person, sport),
where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that person likes
Express the following queries in SQL:
List the people who practice at list one sport they like
List the people who practice at least one sport they do not like
List pairs of people who practice at least one common sport
List the people who like all the sports they practice
List the people who practice all the sports they like
List the people who practice all the sports John likes.

Homework Answers

Answer #1

1.List the people who practice at least one sport they like -

Answer:- SELECT DISTINCT person FROM

PRACTICE P INNER JOIN "LIKE" L ON (P.person=L.person AND P.sport=L.sport);

EXPLANATION:-

Here, we are using SELECT DISTINCT is used to get the distinct result from the resultant table.

Wea re using AS(ALIAS) for renaming columns.

We are using INNER JOIN to combine both the tables ON condition of both the person and the sport should match between the tables which means that the person practise at least one sport he likes.

2.List the people who practice at least one sport they do not like

Answer:-SELECT person FROM

((SELECT person,COUNT(sport) as No_LikePractised FROM

PRACTICE P INNER JOIN "LIKE" L ON (P.person=L.person AND P.sport=L.sport

GROUP BY person) AS PractisedLiked

INNER JOIN

(SELECT person,COUNT(sport) as Liked FROM PRACTICE GROUP BY person) as PractisedCount ON

(PractisedCount.Practised <>PractisedLiked.No_LikePractised AND PractisedCount.person=PractisedLiked.person)

);

EXPLANATION:-

Here ,in first sub query we count the common sporst for a person in both the LIKE and Person table.

For getting this count we are uusing COUNT() aggregate function along with GROUP BY clause.

Then we combine the result of this table created with the result created with second sub query on basis of count and person.

In second sub query we are getting the count of all the practised sport for a person.

In main query we fetch the person from the resultant table.

So , the count of liked and practised sport is not equal to all the practised sport by the person.

Which means person practice at least one sport he do not like.

3.List pairs of people who practice at least one common sport

Answer:-SELECT DISTINCT P1.person AS Person1,P2.person AS Person2 FROM

PRACTICE P1 INNER JOIN PRACTICE P2 ON (P1.person<>P2.person AND P1.sport=P2.sport);

EXPLANATION:-

Here, we are using SELF INNER JOIN on preactice table .

In the join condition we are checking for person to be not same and sport to be same .

Then we are fetching both prsons from the resultant table.

4.List the people who like all the sports they practice

Answer:-SELECT person FROM

((SELECT person,COUNT(sport) as No_LikePractised FROM

PRACTICE P INNER JOIN "LIKE" L ON (P.person=L.person AND P.sport=L.sport

GROUP BY person) AS PractisedLiked

INNER JOIN

(SELECT person,COUNT(sport) as Liked FROM PRACTICE GROUP BY person) as PractisedCount ON

(PractisedCount.Practised =PractisedLiked.No_LikePractised AND PractisedCount.person=PractisedLiked.person)

);

EXPLANATION:-

Here ,in first sub query we count the common sporst for a person in both the LIKE and Person table.

For getting this count we are uusing COUNT() aggregate function along with GROUP BY clause.

Then we combine the result of this table created with the result created with second sub query on basis of count and person.

In second sub query we are getting the count of all the practised sport for a person.

In main query we fetch the person from the resultant table.

So , the count of liked and practised sport is equal to all the practised sport by the person.

5.List the people who practice all the sports they like

SELECT person FROM PRACTICE

((SELECT person,COUNT(sport) as No_LikePractised FROM

PRACTICE P INNER JOIN "LIKE" L ON (P.person=L.person AND P.sport=L.sport

GROUP BY person) AS PractisedLiked

INNER JOIN

(SELECT person,COUNT(sport) as Practised FROM "LIKE" GROUP BY person) as LikedCount ON

(LikedCount .Practised =PractisedLiked.No_LikePractised AND LikedCount.person=PractisedLiked.person)

);

EXPLANATION:-

Here ,in first sub query we count the common sporst for a person in both the LIKE and Person table.

For getting this count we are uusing COUNT() aggregate function along with GROUP BY clause.

Then we combine the result of this table created with the result created with second sub query on basis of count and person.

In second sub query we are getting the count of all the liked sport for a person.

So , the count of liked and practised sport is equal to all the liked sport by the person.

6.List the people who practice all the sports John likes.

Answer:-

SELECT person FROM PRACTICE

WHERE sport IN (SELECT sport FROM "LIKE" WHERE UPPER(person)=UPPER("John"))

GROUP BY person

HAVING COUNT(sport) = (SELECT COUNT(sport) FROM "LIKE" WHERE UPPER(person)=UPPER("John");

EXPLANATION:-

In query first we are getting the allthe person from PRACTICE table who have practise similar sports to the liked sports of John.

John can like multiple sports so here multiple records will be there for a person.

Then , we check the count of sports for a person to be equal to the count of sports LIKED by john.

So, we get the person who practise all the sports liked by john.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that person likes Express the following queries in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that person likes Express the following queries in...
LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE...
LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that person likes Express the following queries in Calculus List pairs of people...
Consider the following relational schema (the primary keys are underlined and foreign keys are italic) ITEM(ItemName,...
Consider the following relational schema (the primary keys are underlined and foreign keys are italic) ITEM(ItemName, ItemType, ItemColour) DEPARTMENT(Deptname, DeptFloor, DeptPhone, Manager) EMPLOYEE(EmpNo, EmpFname, EmpSalary, DeptName, SupervisedBy) SUPPLIER(SupNo, SupName) SALE(SaleNo, SaleQty, ItemName, DeptName) DELIVERY(DeliNo, DeliQty, ItemName, DeptName, SupNo) Write the SQL statements for the following queries: C1. Find the names of items sold on first and second floors. [1 mark] C2. For each department, list the department name and average salary of the employees where the average salary of the...
Relational Algebra (50 pts): Consider the following database schema that keeps track of Sailors, Boats and...
Relational Algebra (50 pts): Consider the following database schema that keeps track of Sailors, Boats and the boats reserved by sailors. Sailors(sid, sname, rating, age) Boatsbid, bname, color) Reserves(sid, bid, date) Keys are underlined in each relation. Specify the following queries in Relational Algebra using above database schema. (f) find the name of sailors with the highest rating (g) find the name and age of oldest sailors (h) find the age of youngest sailor for each rating level (i) find...
Practice writing SQL queries ----------------------------- For this question, we will consider the following tables about people,...
Practice writing SQL queries ----------------------------- For this question, we will consider the following tables about people, grades, and courses. Your task is to express each of the given queries in SQL. Example data are provided in these tables for your convenience but note that they are only example data. Your queries must work for all potential data residing in the given tables, not just those illustrated here. People (id, name, age, address) --------------------------------------------------- p1 | Tom Martin| 24 | 11,...
Problem 8.2 c from Databases Illuminated Third Edition:Assume you have a statistical database with the following...
Problem 8.2 c from Databases Illuminated Third Edition:Assume you have a statistical database with the following schema. The only legal queries are those involving COUNT, SUM, and AVERAGE. newFaculty(facId, lastName, firstName, department, salary, rank, dateHired) a. Write a legal SQL query to find the salary of the only faculty member who is an instructor in the Art department. b. Assume the system will refuse to answer queries for which only one record satisfies the predicate as in (a). Write a...
The relational schema for the Academics database is as follows: DEPARTMENT(deptnum, descrip, instname, deptname, state, postcode)...
The relational schema for the Academics database is as follows: DEPARTMENT(deptnum, descrip, instname, deptname, state, postcode) ACADEMIC(acnum, deptnum*, famname, givename, initials, title) PAPER(panum, title) AUTHOR(panum*, acnum*) FIELD(fieldnum, id, title) INTEREST(fieldnum*, acnum*, descrip) Some notes on the Academics database: An academic department belongs to one institution (instname) and often has many academics. An academic only works for one department. Research papers (PAPER) are often authored by several academics, and of course an academic often writes several papers (AUTHOR). A research field...
Consider the following problem domain. What sort of data store (mongodb vs an enterprise grade sql...
Consider the following problem domain. What sort of data store (mongodb vs an enterprise grade sql database like mysql) should you use and why? Your domain: You are building a web based user interface to allow people to play fantasy sports team games. Your data store (mongo or mysql) will store all of the data needed for your fantasty sports league of choice (clearly you need to specify your sport since competitive table tennis [a thing in other parts of...
Using the Company database in Oracle, construct SQL queries for the following (note: I will make...
Using the Company database in Oracle, construct SQL queries for the following (note: I will make the files to create and populate the Company database available on Isadore shortly): List the last name and address of managers who have a dependent with the same first name as themselves. Retrieve the names of all employees who are directly supervised by ‘Franklin Wong’. Retrieve the names of employees in the Research department who work more than 20 hours per week on the...