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 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 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...
Database design of cothing Online Shopping System. Schema represents minimal information required to store information of...
Database design of cothing Online Shopping System. Schema represents minimal information required to store information of a shop and products to sell. Cart information can be stored in session or if wish-list / watchlist is needed, the schema can be simply extended. Enjoy. SQL and methodology.
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 a person likes Express the following queries...
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 Retail Store database which store the details of different items available in the store and...
Consider Retail Store database which store the details of different items available in the store and the sales of these items to different customers: Schema: Item(ItemNo, ItemName, Category, UnitPrice) Sales(SalesNo, ITemNo, SalesDate, CustomerNo, Qty) Customer(CustomerNo, CustomerName, City, Income, MobileNo) Write the following queries in Relational Algebra: a) List the No. and Name of items in ‘ABC’ category. b) Count the No. of categories from which the items were bought by the customer ‘Mohan Kumar’ c) List the customers who has...
A company database needs to store information about employees (identified by ssn, with salary and phone...
A company database needs to store information about employees (identified by ssn, with salary and phone as attributes), departments (identified by dno, with dname and budget as attributes), and children of employees (with name, age, and relationship to the employee as attributes). Employees work in departments; each department is managed by an employee; a child must be identified uniquely by name when the parent (who is an employee; assume that only one parent works for the company) is known. We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT