Question

In: Computer Science

Given the following relational schema, write queries in SQL to answer the English questions. There is...

Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking.

Customer(cid: integer, cname: string, address: string, city: string, state: string)
Product(pid: integer, pname: string, price: currency, inventory: integer)
Shipment(sid: integer, cid: integer, shipdate: Date/Time)
ShippedProduct(sid: integer, pid: integer, amount: integer)
  1. Return the products (name) whose name contains 'Ch' with a price more than the average price.

    Output:

    +--------------+
    | pname        |
    +--------------+
    | Wooden Chair |
    +--------------+
    
  2. Return all customers and their states that share a state with another customer.

    Output: (Note: Order of rows does not matter.)

    +-----------------+-------+
    | cname           | state |
    +-----------------+-------+
    | Fred Smith      | IL    |
    | Joe Smithsonian | IA    |
    | Steve Stevenson | IL    |
    | Russell Johnson | CA    |
    | John Doe        | MI    |
    | Scott Charles   | CA    |
    | Shannon Rose    | MI    |
    | Beth Rosebud    | IA    |
    | Suzanne May     | IA    |
    +-----------------+-------+
    
  3. Return the shipment id and total value of the entire shipment (price*amount) ordered by the shipment values ascending.

    Output:

    +-----+----------------+
    | sid | Total_Shipment |
    +-----+----------------+
    |  12 |          45.49 |
    |   8 |          98.75 |
    |   7 |          98.97 |
    |  10 |         104.00 |
    |   4 |         164.95 |
    |   6 |         183.96 |
    |  11 |         260.00 |
    |   3 |         659.80 |
    |   5 |         676.00 |
    |   9 |        1664.00 |
    +-----+----------------+

Solutions

Expert Solution

1) SELECT pname FROM product WHERE pname LIKE "%Ch%" AND price > AVG(price);

Note:

  • Here, we can see one aggregate function AVG() is used.
  • We have to select pnames contains Ch, so use a LIKE operator associated with pname. %Ch% specifies zero or more characters on the beginning or end of pname.
  • Two conditions, pname with 'ch' and also the price is equal to average of all the price are satisfied, then that rows will be selected.

2)SELECT cname, state FROM Customer A, Customer B WHERE A.cid != B.cid AND A.state= B.state;

  • We can use same table with different aliase names in a same query. A and B are aliases of same table Customer.
  • Here, we have to check a customer name with same state, so use the condition A.state = B.state also both will not representing same customer so use A.cid not equal to B.cid.

3)SELECT sid, price*amount AS "Total_Shipment" FROM Product, ShippedProduct WHERE Product.pid= ShippedProduct.pid ORDER BY price*amount asc;

  • Two tables Product and ShippedProduct, we can calculate price* amount. Use an aliase name Total_Shipment for price*amount.
  • If the Pid's both tables are same, then price from Product and amount from ShippedProduct will be multiplied.
  • Also the result should be in the ascending order of price*amount.
  • Use asc keyword for ascending, desc for descending order sorting.

Related Solutions

Given the following relational schema, write queries in SQL to answer the English questions. There is...
Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid: integer, shipdate: Date/Time) ShippedProduct(sid:...
Write SQL queries for the following statements based on Employees table whose schema is given below:...
Write SQL queries for the following statements based on Employees table whose schema is given below: (Employee_ID,First_Name,Last_Name,Email,Phone_Number,Hire_Date,Job_ID,Salary, Manager_Id, Department_Id) a. Create the given table along with the following constraints: Phone_Number should not be left undefined, salary should be between 10000 and 20000, employee_id should uniquely identify rows and should not be left undefined. User-defined names should be given to constraints. b.  Display each employee’s last name, hire date and salary review date (which is the date after six months of service)....
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
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 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 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...
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)...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT