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 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...
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...
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...
1. Consider the following tables in a relational database. Provide the appropriate "SELECT" SQL statement necessary...
1. Consider the following tables in a relational database. Provide the appropriate "SELECT" SQL statement necessary to answer the queries that follow. Primary keys are underlined and foreign key fields have an asterisk at the end of the field. CUSTOMERS (CUST-NO, C-NAME, C-ADDRESS, BALANCE) SALESPERSONS (SP-NO, S-NAME, DATE-EMPLOYED, SALARY) SALES (INVOICE-NO, DATE, CUST-NO*, SP-NO*) a) List the salesperson name and salary for all sales to customers whose balance outstanding is greater than 20000. b) List the names and addresses of...
Summary The Ch08_ConstructCo database stores data for a consulting company that tracks all charges to projects....
Summary The Ch08_ConstructCo database stores data for a consulting company that tracks all charges to projects. The charges are based on the hours each employee works on each project. The structure and contents of the Ch08_ConstructCo database are shown in Figure P8.1. Use this database to answer the following problems. Database Schema The schema for the Ch08_ConstructCo database is shown below and should be used to answer the next several problems. Click this image to view it in its own...
***This is a complete question. Do not tag this as incomplete. Write SQL queries to the...
***This is a complete question. Do not tag this as incomplete. Write SQL queries to the below Relational Database. • Regarding the SQL queries: • Do not use SELECT * in any query. • Do not use any SQL features (including Nullif) that were not covered in this course. • Do not use subqueries IF joins can be used to answer a question. However, some questions may require the use of subqueries. The Movie Database Notes: TheaterNum, MovieNum, and ActorNum...
The following is about databases: queries on relational algebra Consider the following schemas for a LinkedIn-like...
The following is about databases: queries on relational algebra Consider the following schemas for a LinkedIn-like professional network. Note this is a slightly simplified version from your last assignment. Assume that the connections are mutual. Person (PID, first_name, last_name, age, gender) School (school_ID, school_name, type, city, state) Education (PID, school_ID, degree, major, year) Company (company_ID, company_name, total_asset, headquarter) Branch (company_ID, branch_num, city, state) WorksAt (PID, company_ID, branch_num, start, end) Connection (PID, friend_ID) For each relation, the attribute(s) of the primary...
Garden Glory Project Questions Assume that Garden Glory designs a database with the following tables: OWNER...
Garden Glory Project Questions Assume that Garden Glory designs a database with the following tables: OWNER (OwnerID, OwnerName, OwnerEmail, OwnerType) OWNED_PROPERTY (PropertyID, PropertyName, PropertyType, Street, City, State, Zip, OwnerID) GG_SERVICE (ServiceID, ServiceDescription, CostPerHour); EMPLOYEE (EmployeeID, LastName, FirstName, CellPhone, ExperienceLevel) PROPERTY_SERVICE ( PropertyServiceID , PropertyID , ServiceID, ServiceDate , EmployeeID, HoursWorked) The referential integrity constraints are: OwnerID in OWNED_PROPERTY must exist in OwnerID in OWNER PropertyID in PROPERTY_SERVICE must exist in PropertyID in OWNED_PROPERTY ServiceID in PROPERTY_SERVICE must exist in ServiceID...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT