Question

In: Computer Science

Consider the following database schema: Frequents(kid, store) Sells(store, candy) Likes(kid, candy) Stores(store, city, phone) Kids(kid, city,...

Consider the following database schema:
Frequents(kid, store)
Sells(store, candy)
Likes(kid, candy)
Stores(store, city, phone)
Kids(kid, city, age)
Write relational algebra expression(s) to:
a) Find the stores in ’Warrensburg’, for each display only the store name and phone
number.
b) Find the stores in ’Warrensburg’, which sells ’Hersheys’ or ’Mars’. Your
expression must use set union.
c) Repeat the above without using set union.
d) Find the kids who are 10 years or older and like ’M&Ms’.
e) Find the kids and stores in the same city. Display the city, kid, and store names.
f) Find the kids who are frequents at a store which sells at least one candy they like. Display
the kid and store names.
g) Find the kids who like ’Mars’ but not ’M&Ms’. (Hint: set difference)

Solutions

Expert Solution

--a

select store,phone

from Stores

where city = 'Warrenburg';

--explanation:

--select statement retreives the column names store and phone

--where statement defines a condition that city name is Warrenburg

--b

select Sells.store

from Sells,Stores

where Sells.store = Stores.store and candy = "Hersheys" and city = "Warrenburg"

UNION

select Sells.store

from Sells,Stores

where Sells.store = Stores.store and candy = "Mars" and city =  "Warrenburg";

--explanation:

--The first statement before union retrieves the stores selling Hersheys

--The second statement after union retreives the stores selling Mars

--union retireives all the results also if any duplicates found they're neglected

--C

select Sells.store

from Sells,Stores

where Sells.store = Stores.store  and city =  "Warrenburg" and (candy = "Mars" or candy = "Hersheys");

--explanation:

--instead of UNION we are using a OR statement in where conditional statement

--d

select Kids.kid

from Kids,Likes

where Kids.Kid = Likes.kid and candy = "M&Ms" and age >= 10;

--explanation:

--in the conditional statement we are specifying kid who likes M&Ms candies by equalizing the corresponding kid in Kids table

--e

select Kids.city,Kids.kid,Stores.store

from Kids,Stores

where Kids.store = Stores.store and Kids.city = Stores.city

--explanation:

--In where statement we're checking for same city and equalizing the same stores

--f

select Frequents.kid,Frequents.store

from Frequents,Likes,Sells

where Frequents.store = Sells.store and Sells.candy = Likes.candy and Likes.kid = Frequents.kid

group by Sells.candy

HAVING count(candy) >= 1

--explanation:

--first we group by candy so that we can now how much a store is selling

--having count makes sure to include the stores which sells atleast one candy

--remaining where condition says about corresponding kid and candy they like

--g

select kid

from Likes

where candy = "Mars"

MINUS

select kid

from Likes

where candy = "M&Ms";

--first find kids liking Mars And make a set difference using MINUS so that it retrieves only kids which does not like M&Ms but likes mars


Related Solutions

Consider the following relational database schema:             employee(employee-name, employee-id, street, e-city)             works(employee-
Consider the following relational database schema:             employee(employee-name, employee-id, street, e-city)             works(employee-id, company-id, salary)             company(company-name, company-id, c-city)             manages(employee-id, manager-id) Specify the following queries on this database schema using the relational operators we discussed in class. Write your answers on a separate sheet of paper in the order that they are specified below. Retrieve the name and address of employees who work for First Bank Corporation. Retrieve the name, street address, and city of residence of all employees...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition,...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition, price), PK: ISBN, FK: published_by refs Publisher, previous_edition refs Book. Author (SSN, first_name, last_name, address, income), PK: SSN. Write (aSSN, bISBN), PK: (aSSN, bISBN), FK: aSSN refs Author, bISBN refs Book. Editor (SSN, first_name, last_name, address, salary, works_for, book_count), PK: SSN, FK: works_for refs Publisher. Edit (eSSN, bISBN), PK: (eSSN, bISBN), FK: eSSN refs Editor, bISBN refs Book. Author_Editor (aeSSN, hours), PK: aeSSN, FK:...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition,...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition, price), PK: ISBN, FK: published_by refs Publisher, previous_edition refs Book. Author (SSN, first_name, last_name, address, income), PK: SSN. Write (aSSN, bISBN), PK: (aSSN, bISBN), FK: aSSN refs Author, bISBN refs Book. Editor (SSN, first_name, last_name, address, salary, works_for, book_count), PK: SSN, FK: works_for refs Publisher. Edit (eSSN, bISBN), PK: (eSSN, bISBN), FK: eSSN refs Editor, bISBN refs Book. Author_Editor (aeSSN, hours), PK: aeSSN, FK:...
Need an example of a database schema for an online or ecommerce store and an explanation...
Need an example of a database schema for an online or ecommerce store and an explanation of the how it functions
Consider the following universal relation THE following database schema is in 4NF. What can you infer...
Consider the following universal relation THE following database schema is in 4NF. What can you infer about multi-valued dependencies? A C D A B C E E F A-It does not have multi-valued dependencies. B-The multi-valued dependency A ->-> C does not hold. If the multi-valued dependency A ->-> C held, the database would not be in 4NF. C-The multi-valued dependency A ->-> B does not hold. If the multi-valued dependency A ->-> B held, the database would not be...
Consider the following database schema:                Product(maker, model, type)                PC(model, speed
Consider the following database schema:                Product(maker, model, type)                PC(model, speed, ram, hd, rd, price)                Laptop(model, speed, ram, hd, screen, price)                Printer(model, color, type, price) Give SQL statement for each of the following: (Grouping and Aggregation) Write the following queries in SQL: Find the average speed of laptops costing over $2000. Find the average price of PC’s and laptops made by manufacturer “D”. Find, for each manufacturer, the average screen size of its laptops. Find the manufacturers...
Consider the following database schema:                Product(maker, model, type)                PC(model, speed
Consider the following database schema:                Product(maker, model, type)                PC(model, speed, ram, hd, rd, price)                Laptop(model, speed, ram, hd, screen, price)                Printer(model, color, type, price) Give SQL statement for each of the following: (Subqueries, Join operations) Write the following queries in SQL: Find the makers of PC’s with a speed of at least 1200. Find the printers with the highest price. Find the laptops whose speed is lower than that of any PC. Find the model number...
SQL query exercises: Consider the following database schema:                Product(maker, model, type)              &
SQL query exercises: Consider the following database schema:                Product(maker, model, type)                PC(model, speed, ram, hd, rd, price)                Laptop(model, speed, ram, hd, screen, price)                Printer(model, color, type, price) Consider the Drivers-Cars-Reserves DB for a small rental car company:                Drivers(id, name, rating, age)                Cars(vin, maker, model, year, color)                Reserves(did, vin, date) Give SQL statement each of the following operations: Find the colors of cars reserved by Smith. Find all id’s of drivers who have a...
Question 2: consider the following library relational database schema Write a program segment to retrieves the...
Question 2: consider the following library relational database schema Write a program segment to retrieves the list of books that became overdue yesterday and that prints the book title and borrower name for each. 1- Use JDBC with Java as the host language
[Q.4] Answer the following questions You are invited as a database architect to develop database schema...
[Q.4] Answer the following questions You are invited as a database architect to develop database schema for maintaining patient information for the NYU medical group (make necessary assumptions for the data requirements if needed). Each physician in the Lehman medical group is uniquely identified by physicianID o Each physician must have first name, and last name, and phone number Each patient is identified by patientID o Each patient must have first name, and last name, phone number, and insurance card...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT