Question

Given the following schema:

Furniture f id category style price int char(20) char(15) dec(5,2) primary key not null values restricted to modern, rustic, traditional not null [Example: sofa, bed,...] Per week rental cost] Customers c id name phone email int char(10) char(13) char(15) primary key not null Checkout c id f id ck out_date date ret date foreign key, references c id of customers foreign key, references f id of furniture not null int int date [c id and f id together form the primary key]

I would like to know the SQL queries to:

1. Total up the number of checkouts made on each month of the year. Then print the month (spelled out completely) and the total number of corresponding checkouts next to it. That is, print "January" and total number of checkouts made in January, and so on.

2. Show the furniture styles that are checked-out on Sundays only. That is, none of the furniture in that style must have been checked out on any other days.

3. Find names of people who have checked out any piece of furniture within the last 15 days (not counting the day the query is being run) but have not returned that yet. Print their names and the string "* To be contacted" next to each name.

4. For all the furniture that were checked out in October 2018 and returned in November 2018, print their F_ID, category and style. However, while printing style, print 'M' for 'modern', 'R' for 'rustic', and 'T' for 'traditional'.

5. Assume that the price attribute of Furniture table indicates weekly (7 days) rental cost. Write a view to calculate the total money earned from all "rustic" style furniture, which has been rented and returned (i.e., do not take into consideration the ones that are yet to be returned). Print the amount.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*1. Total up the number of checkouts made on each month of the year.

Then print the month (spelled out completely) and the total number of corresponding checkouts next to it.

That is, print "January" and total number of checkouts made in January, and so on.*/

--sql serve

SELECT FORMAT(ck_out_date, 'MMMM') AS Month,COUNT(c_id) AS TotalNumberOfCheckOuts FROM Check_out GROUP BY FORMAT(ck_out_date, 'MMMM');

--Mysql

SELECT MONTHNAME(ck_out_date) AS Month,COUNT(c_id) AS TotalNumberOfCheckOuts FROM Check_out GROUP BY MONTHNAME(ck_out_date);

/*2. Show the furniture styles that are checked-out on Sundays only.

That is, none of the furniture in that style must have been checked out on any other days.*/

--Msql

SELECT f.style, FROM Furniture f INNER JOIN Check_out c on f.f_id=c.f_id

WHERE DAYNAME(c.ck_out_date)='SUNDAY';

--SQLSERVER

SELECT f.style, FROM Furniture f INNER JOIN Check_out c on f.f_id=c.f_id

WHERE datename(dw,c.ck_out_date)='SUNDAY';

/*3. Find names of people who have checked out any piece of furniture within the last 15 days

(not counting the day the query is being run) but have not returned that yet.

Print their names and the string "* To be contacted" next to each name.*/

--SQL SERVER

SELECT c.name FROM customers c INNER JOIN Check_out co ON c.c_id=co.c_id

WHERE convert(date,co.ck_out_date)>=convert(date,GETDATE()-16)

AND co.ck_out_date is not null

--Mysql

SELECT c.name FROM customers c INNER JOIN Check_out co ON c.c_id=co.c_id

WHERE convert(date,co.ck_out_date)>=convert(subdate(NOW(), 16),date)

AND co.ck_out_date is not null

/*4. For all the furniture that were checked out in October 2018 and returned in November 2018,

print their F_ID, category and style. However, while printing style,

print 'M' for 'modern', 'R' for 'rustic', and 'T' for 'traditional'.*/

--MYSQL

SELECT c.F_ID, (CASE WHEN style = 'modern' THEN 'M'

WHEN style = 'rustic' THEN 'R'

WHEN style = 'traditional' THEN 'T'

END )AS Style

FROM Furniture f INNER JOIN Check_out c on f.f_id=c.f_id

WHERE (MONTHNAME(c.ck_out_date) ='October' AND YEAR(c.ck_out_date)=2018)

AND (MONTHNAME(c.ret_date) ='November' AND YEAR(c.ret_date)=2018)

--SQL SERVER

SELECT c.F_ID, (CASE WHEN style = 'modern' THEN 'M'

WHEN style = 'rustic' THEN 'R'

WHEN style = 'traditional' THEN 'T'

END )AS Style

FROM Furniture f INNER JOIN Check_out c on f.f_id=c.f_id

WHERE (FORMAT(c.ck_out_date, 'MMMM') ='October' AND YEAR(c.ck_out_date)=2018)

AND (FORMAT(c.ret_date, 'MMMM') ='November' AND YEAR(ret_date)=2018)

/*5. Assume that the price attribute of Furniture table indicates weekly (7 days) rental cost.

Write a view to calculate the total money earned from all "rustic" style furniture,

which has been rented and returned

(i.e., do not take into consideration the ones that are yet to be returned). Print the amount.*/

-- Mysql

SELECT SUM(f.price * (timestampdiff(DAY,c.ck_out_date,c.ret_date)/7))

FROM Furniture f INNER JOIN Check_out c on f.f_id=c.f_id

WHERE f.style='rustic' and c.ret_date is not null

GROUP BY f.style

--Sql server

SELECT SUM(f.price * (DATEDIFF(DAY,c.ck_out_date,c.ret_date)/7))

FROM Furniture f INNER JOIN Check_out c on f.f_id=c.f_id

WHERE f.style='rustic' and c.ret_date is not null

GROUP BY f.style

Add a comment
Know the answer?
Add Answer to:
Given the following schema: I would like to know the SQL queries to: 1. Total up...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects...

    MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects 2.List the project with the most hours SCRIPT TO BUILD DATABASE create table department ( dept_ID int not null , dept_name char(50) NOT NULL, manager_ID int not null, manager_start_date date not null, constraint Dept_PK primary key (dept_ID), constraint D_Name_AK unique (dept_name) ); insert into department values(1,'abc',1,'2019-01-08'); insert into department values(2,'abc2',2,'2019-01-08'); insert into department values(3,'abc3',2,'2019-01-08'); insert into department values(4,'abc4',2,'2019-01-08'); /*project table done*/ create table project...

  • I need help with the following SQL query for a company database (script given below). The...

    I need help with the following SQL query for a company database (script given below). The name of the Department. The number of employees working in that department. The number of different projects controlled by this department. The name of the project controlled by this department that has the maximum number of employees of the company working on it. The number of the above project. The cumulative sum of the number of employees of the company working on the projects...

  • Given the following table structure, write the SQL code to display all the first and last...

    Given the following table structure, write the SQL code to display all the first and last names of the individuals that have a last name that begins with the letter 'M! CREATE TABLE Persons Personi int LastName varchar(80). FirstName varchar(80). Address varchar(125). State char(2) Given the following table structure, write the SQL code to display all the first and last names of the individuals that have a last name that does not contain the letter 'S: CREATE TABLE Persons PersonlDint,...

  • Database HW help!!!!!!! Provide the SQL statements for the following queries. (1) Provide the names and...

    Database HW help!!!!!!! Provide the SQL statements for the following queries. (1) Provide the names and phones of all swimmers currently in level (of id) 3. +-------+---------+---------------------------+ | FName | LName   | EMail                     | +-------+---------+---------------------------+ | Bobby | Khan    | theBKhan1 | | Clara | Johnson | ClaraJohnson_11 | +-------+---------+---------------------------+ 2 rows in set (0.00 sec) (2) Provide the names of swimmers who have signed up to participate in the event '100M Butterfly' of Meet id 1. +-------+---------+ | FName...

  • SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT...

    SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT NULL, PRIMARY KEY (vid) ); CREATE TABLE category ( catid CHAR(2) NOT NULL, catname VARCHAR(25) NOT NULL, PRIMARY KEY (catid) ); CREATE TABLE product ( pid CHAR(3) NOT NULL, pname VARCHAR(25) NOT NULL, price NUMERIC (7,2) NOT NULL, vid CHAR(2) NOT NULL, categoryid CHAR(2) NOT NULL, PRIMARY KEY (pid)); CREATE TABLE region ( rid CHAR NOT NULL, rname VARCHAR(25) NOT NULL, PRIMARY KEY (rid)...

  • I NEED TO WRITE THE FOLLOWING QUERIES IN MYSQL (13)Next, grant to a user admin the read privileges on the complete descr...

    I NEED TO WRITE THE FOLLOWING QUERIES IN MYSQL (13)Next, grant to a user admin the read privileges on the complete descriptions of the customers who submitted no orders. For example, these are the customers who registered themselves and submitted no orders so far. The granted privilege cannot be propagated to the other users. 0.3 (14)Next, grant to a user admin the read privileges on information about the total number of orders submitted by each customer. Note, that some customers...

  • Use only a single SQL statement for each of the following questions 1 Give a listing of all the ssns,first names and the class descriptions of all the classes the students are taking. If there a...

    Use only a single SQL statement for each of the following questions 1 Give a listing of all the ssns,first names and the class descriptions of all the classes the students are taking. If there are no class _descriptions display 'No description is available yet'. (USE NVL) 2 Give a listing of only the lname and the class_code for students who are taking 'Introduction to C programming'. (Inner join) 3 Give a lising of all the class_descriptions and the number...

  • Ch 1 1. Given the following dat Dec 31 Year 2 Dec 31 Year 1 Total...

    Ch 1 1. Given the following dat Dec 31 Year 2 Dec 31 Year 1 Total liabilities S128,250 $120,000 Total stockholders oquity 95.000 80.000 compute the ratio of liabilities to stockholders' equity for each year Round to two decimal places 1.50 and 107, 11.35 and 1.50 respectively respectively 1.07 and 1.19. 1.1.19 and 1.35 respectively respectively The liabilities and stockholder's equity of a company are $132,000 and $244.000, respectively. Assets should equal SS188.00 $132.00 p $376,00 12.000 A financial statement...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT