Question

In: Computer Science

Write the following questions as queries in SQL. Use only the operators discussed in class (no...

Write the following questions as queries in SQL. Use only the operators discussed in class (no outer joins)

Consider the following database schema:

INGREDIENT(ingredient-id,name,price-ounce)

RECIPE(recipe-id,name,country,time)

USES(rid,iid,quantity)

where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid is a foreign key to RECIPE and iid is a foreign key to INGREDIENT

Write the following queries in SQL.

1. Find the names of French recipes that use butter and take longer than 45 minutes to cook.

2. Find the names of recipes that use butter or lard.

3. Find the names of recipes that use butter and lard.

Solutions

Expert Solution

1. Find the names of French recipes that use butter and take longer than 45 minutes to cook.

select r.name from RECIPE r, USES u, INGREDIENT i where lower(r.country) = 'france' and r.time > 45 and r.recipe-id = u.rid and u.iid = i.ingredient-id and lower(i.name) = 'butter';

The above returns the recipe names of those recipe whose country is france (French recipe) and takes longer than 45 minutes to cook (assuming time in recipe table are specified in minutes) and then it natural joins with uses table on field recipe-id which joins with ingredient table on field ingredient-id to find the recipe that uses butter.

2.

Find the names of recipes that use butter or lard.

select r.name from RECIPE r, USES u , INGREDIENT i where r.recipe-id = u.rid and u.iid = i.ingredient-id and lower(i.name) = 'butter'
UNION
select r.name from RECIPE r, USES u , INGREDIENT i where r.recipe-id = u.rid and u.iid = i.ingredient-id and lower(i.name) = 'lard';

The first query returns the name all recipes that uses butter and the second query returns the name of all recipes that uses lard. The UNION operator combines the result of both the queries and returns the name of all recipes that uses butter or lard.

3.

Find the names of recipes that use butter and lard.

select r.name from RECIPE r, USES u , INGREDIENT i where r.recipe-id = u.rid and u.iid = i.ingredient-id and lower(i.name) = 'butter'
INTERSECT
select r.name from RECIPE r, USES u , INGREDIENT i where r.recipe-id = u.rid and u.iid = i.ingredient-id and lower(i.name) = 'lard';

The first query returns the name all recipes that uses butter and the second query returns the name of all recipes that uses lard. The INTERSECT operator returns the name which are present in both the queries i.e returns the name of those recipes that uses butter and lard.


Related Solutions

Write the following questions as queries in SQL. Use only the operators discussed in class (no...
Write the following questions as queries in SQL. Use only the operators discussed in class (no outer joins) Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid is a foreign...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). The following database schema is given: ATHLETE(name,age,height,weight,country) RACE(id,location,date,time-start,distance) COMPETES(aname,rid,time,position) where ATHLETE has information about runners (their name, age, height, weight, and nationality); RACE has information about races (id, location, date when it’s held, time it starts, and distance ran); and COMPETES keeps track of which runners run on with race, the time it...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). Type your answers. The following database schema is given: ATHLETE(name,age,height,weight,country) RACE(id,location,date,time-start,distance) COMPETES(aname,rid,time,position) where ATHLETE has information about runners (their name, age, height, weight, and nationality); RACE has information about races (id, location, date when it’s held, time it starts, and distance ran); and COMPETES keeps track of which runners run on with race, the time it took them to complete the race,...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference...
Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). Type your answers. Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid...
Write the following SQL queries and show the corresponding output of the DBMS: 1) Write an...
Write the following SQL queries and show the corresponding output of the DBMS: 1) Write an SQL statement to display all the information of all Nobel Laureate winners. 2) Write an SQL statement to display the string "Hello, World!". 3) Write an SQL query to display the result of the following expression: 2 * 14 +76. 4) Write an SQL statement to display the winner and category of all Laureate winners. 5) Write an SQL query to find the winner(s)...
In this assignment, you are required to write the SQL statements to answer the following queries...
In this assignment, you are required to write the SQL statements to answer the following queries using PostgreSQL system. The SQL statements comprising the DDL for Henry Books Database are given to you in two files. For that database, answer the following queries. Create the files Q1 to Q10 in PostgreSQL. Do follow the restrictions stated for individual queries. 1. List the title of each book published by Penguin USA. You are allowed to use only 1 table in any...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a select statement to show the invoicelineitemdescriptions that have the total invoicelineitemamount >1000 and the number of accountno is >2. 10. Write a select statement that returns the vendorid, paymentsum of each vendor, and the number of invoices of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number...
Basic SQL Use the following schema to answer the queries below using SQL DEVICE [dno, dtype,...
Basic SQL Use the following schema to answer the queries below using SQL DEVICE [dno, dtype, price] PROVIDER [pno, pname, web] SERVICE [dno, pno, servicedate] SERVICE.dno references DEVICE.dno SERVICE.pno references PROVIDER.pno bold is underline. a) Find the dno for the most expensive device b) Find all providers that have the work fast in the name c) Find the number of different device types (dtype) d) Give all details of devices with price more than $400
Write SQL queries below for each of the following: List the names and cities of all...
Write SQL queries below for each of the following: List the names and cities of all customers List the different states the vendors come from (unique values only, no duplicates) Find the number of customers in California List product names and category descriptions for all products supplied by vendor Proformance List names of all employees who have sold to customer Rachel Patterson
Please use the books database pasted under question 4 to design the following SQL queries. Use...
Please use the books database pasted under question 4 to design the following SQL queries. Use any method such as subqueries, equi-join/inner-join, outer join, EXISTS 1. Find the name(s) of the publisher(s) who have published the computer book. 2. Find the name(s) of the author(s) that have authored more than one books. 3. Find the name(s) of the publisher(s) who published the least expensive book. 4. Find the name(s) of the author(s) who wrote the book with the greatest number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT