Question

In: Computer Science

Write a query to display the columns listed below. For each customer the query should show...

  1. Write a query to display the columns listed below. For each customer the query should show the current system date, the current day (when you do the problem the date and day will be different), the number of characters in the member last name, the last date the customer rented a video and how many total videos the person rented.

/* Database Systems, 9th Ed., Coronel/MOrris/Rob */
/* Type of SQL : MySQL */

CREATE SCHEMA IF NOT EXISTS TINY_VIDEO;

USE TINY_VIDEO;

DROP TABLE IF EXISTS detail_rental;
DROP TABLE IF EXISTS rental;
DROP TABLE IF EXISTS membership;
DROP TABLE IF EXISTS video;
DROP TABLE IF EXISTS movie;
DROP TABLE IF EXISTS price;

/*Create table price*/
CREATE TABLE price
(price_id INTEGER PRIMARY KEY AUTO_INCREMENT,
description VARCHAR(20) NOT NULL,
rental_fee DECIMAL(5,2),
daily_late_fee DECIMAL(5,2));


/*Insert data into price*/
INSERT INTO price VALUES(1,'Standard',2.5,1);
INSERT INTO price VALUES(2,'New Release',4.0,3);
INSERT INTO price VALUES(3,'Discount',2.0,1);
INSERT INTO price VALUES(4,'Weekly Special',1.5,.5);


/*Create table movie*/
CREATE TABLE movie
(movie_id INTEGER PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(75) NOT NULL,
year_released INTEGER,
cost DECIMAL(5,2),
genre VARCHAR(50),
price_id INTEGER,
FOREIGN KEY(price_id) REFERENCES price(price_id));

/*Insert data into movie*/
INSERT INTO movie VALUES(1234,'The Cesar Family Christmas',2007,39.95,'FAMILY',2);
INSERT INTO movie VALUES(1235,'Smokey Mountain Wildlife',2004,59.95,'ACTION',3);
INSERT INTO movie VALUES(1236,'Richard Goodhope',2008,59.95,'DRAMA',2);
INSERT INTO movie VALUES(1237,'Beatnik Fever',2007,29.95,'COMEDY',2);
INSERT INTO movie VALUES(1238,'Constant Companion',2008,89.95,'DRAMA',NULL);
INSERT INTO movie VALUES(1239,'Where Hope Dies',1998,25.49,'DRAMA',3);
INSERT INTO movie VALUES(1245,'Time to Burn',2006,45.49,'ACTION',3);
INSERT INTO movie VALUES(1246,'What He Doesn''t Know',2006,58.29,'COMEDY',1);


/*Create table video*/
CREATE TABLE video
(video_id INTEGER PRIMARY KEY AUTO_INCREMENT,
purchase_date DATE,
movie_id INTEGER,
FOREIGN KEY(movie_id) REFERENCES movie(movie_id));

/*Insert data into video*/
INSERT INTO video VALUES(54321,'2008-06-18',1234);
INSERT INTO video VALUES(54324,'2008-06-18',1234);
INSERT INTO video VALUES(54325,'2008-06-18',1234);
INSERT INTO video VALUES(34341,'2007-01-22',1235);
INSERT INTO video VALUES(34342,'2007-01-22',1235);
INSERT INTO video VALUES(34366,'2009-03-02',1236);
INSERT INTO video VALUES(34367,'2009-03-02',1236);
INSERT INTO video VALUES(34368,'2009-03-02',1236);
INSERT INTO video VALUES(34369,'2009-03-02',1236);
INSERT INTO video VALUES(44392,'2008-10-21',1237);
INSERT INTO video VALUES(44397,'2008-10-21',1237);
INSERT INTO video VALUES(59237,'2009-02-14',1237);
INSERT INTO video VALUES(61388,'2007-01-25',1239);
INSERT INTO video VALUES(61353,'2006-01-28',1245);
INSERT INTO video VALUES(61354,'2006-01-28',1245);
INSERT INTO video VALUES(61367,'2008-07-30',1246);
INSERT INTO video VALUES(61369,'2008-07-30',1246);


/*Create table membership*/
CREATE TABLE membership
(membership_id INTEGER PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
street VARCHAR(120),
city VARCHAR(50),
state VARCHAR(2),
zip VARCHAR(5),
balance DECIMAL(10,2));

/*Insert data into membership*/
INSERT INTO membership VALUES(102,'Tami','Dawson','2632 Takli Circle','Norene','TN','37136',11);
INSERT INTO membership VALUES(103,'Curt','Knight','4025 Cornell Court','Flatgap','KY','41219',6);
INSERT INTO membership VALUES(104,'Jamal','Melendez','788 East 145th Avenue','Quebeck','TN','38579',0);
INSERT INTO membership VALUES(105,'Iva','Mcclain','6045 Musket Ball Circle','Summit','KY','42783',15);
INSERT INTO membership VALUES(106,'Miranda','Parks','4469 Maxwell Place','Germantown','TN','38183',0);
INSERT INTO membership VALUES(107,'Rosario','Elliott','7578 Danner Avenue','Columbia','TN','38402',5);
INSERT INTO membership VALUES(108,'Mattie','Guy','4390 Evergreen Street','Lily','KY','40740',0);
INSERT INTO membership VALUES(109,'Clint','Ochoa','1711 Elm Street','Greenville','TN','37745',10);
INSERT INTO membership VALUES(110,'Lewis','Rosales','4524 Southwind Circle','Counce','TN','38326',0);
INSERT INTO membership VALUES(111,'Stacy','Mann','2789 East Cook Avenue','Murfreesboro','TN','37132',8);
INSERT INTO membership VALUES(112,'Luis','Trujillo','7267 Melvin Avenue','Heiskell','TN','37754',3);
INSERT INTO membership VALUES(113,'Minnie','Gonzales','124 6th Street West','Williston','ND','58801',0);


/*Create table rental*/
CREATE TABLE rental
(rental_id INTEGER PRIMARY KEY AUTO_INCREMENT,
rental_date DATE,
membership_id INTEGER,
FOREIGN KEY(membership_id) REFERENCES membership(membership_id));

/*Insert data into rental*/
INSERT INTO rental VALUES(1001,'2009-03-01',103);
INSERT INTO rental VALUES(1002,'2009-03-01',105);
INSERT INTO rental VALUES(1003,'2009-03-02',102);
INSERT INTO rental VALUES(1004,'2009-03-02',110);
INSERT INTO rental VALUES(1005,'2009-03-02',111);
INSERT INTO rental VALUES(1006,'2009-03-02',107);
INSERT INTO rental VALUES(1007,'2009-03-02',104);
INSERT INTO rental VALUES(1008,'2009-03-03',105);
INSERT INTO rental VALUES(1009,'2009-03-03',111);


/*Create table detailrental*/
CREATE TABLE detail_rental
(rental_id INTEGER,
video_id INTEGER,
fee DECIMAL(5,2),
due_date DATE,
return_date DATE,
daily_late_fee DECIMAL(5,2),
PRIMARY KEY(rental_id, video_id),
FOREIGN KEY(rental_id) REFERENCES rental(rental_id),
FOREIGN KEY(video_id) REFERENCES video(video_id));

/*Insert data into dailyrental*/
INSERT INTO detail_rental VALUES(1001,34342,2,'2009-03-04','2009-03-02',1);
INSERT INTO detail_rental VALUES(1001,61353,2,'2009-03-04','2009-03-03',1);
INSERT INTO detail_rental VALUES(1002,59237,3.5,'2009-03-04','2009-03-04',3);
INSERT INTO detail_rental VALUES(1003,54325,3.5,'2009-03-04','2009-03-09',3);
INSERT INTO detail_rental VALUES(1003,61369,2,'2009-03-06','2009-03-09',1);
INSERT INTO detail_rental VALUES(1003,61388,0,'2009-03-06','2009-03-09',1);
INSERT INTO detail_rental VALUES(1004,44392,3.5,'2009-03-05','2009-03-07',3);
INSERT INTO detail_rental VALUES(1004,34367,3.5,'2009-03-05','2009-03-07',3);
INSERT INTO detail_rental VALUES(1004,34341,2,'2009-03-07','2009-03-07',1);
INSERT INTO detail_rental VALUES(1005,34342,2,'2009-03-07','2009-03-05',1);
INSERT INTO detail_rental VALUES(1005,44397,3.5,'2009-03-05','2009-03-05',3);
INSERT INTO detail_rental VALUES(1006,34366,3.5,'2009-03-05','2009-03-04',3);
INSERT INTO detail_rental VALUES(1006,61367,2,'2009-03-07',NULL,1);
INSERT INTO detail_rental VALUES(1007,34368,3.5,'2009-03-05',NULL,3);
INSERT INTO detail_rental VALUES(1008,34369,3.5,'2009-03-05','2009-03-05',3);
INSERT INTO detail_rental VALUES(1009,54324,3.5,'2009-03-05',NULL,3);
INSERT INTO detail_rental VALUES(1001,34366,3.5,'2009-03-04','2009-03-02',3);

Solutions

Expert Solution

SELECT M.membership_id,CONCAT(M.first_name,' ',M.last_name) AS Name ,CURDATE() AS Current_Date,

DAYNAME(CURDATE()) AS Current_Day,CHAR_LENGTH(M.last_name) AS last_name_length, MAX(R.rental_date) AS Last_date_rented ,COUNT(DR.video_id) as No_of_videos

FROM

MemberShip M INNER JOIN rental R ON R.membership_id =M.memberShip_id

INNER JOIN detail_rental DR ON DR.rental_id =R.rental_id

GROUP BY membership_id;

EXPLANATION:-

Here, we are using SELECT to fetchdata from the table.

CONCAT() is used to combine first and last name to get the full name.

CURDATE() is used to get the todays date.\

DAYNAME() function returns the name of the week's day for particular date passed.

CHAR_LENGTH() calculates the length of the character of the passed string.

COUNT() and MAX() are aggregate functions to ge the count of the videos and maximum rental date which will be last date rented.

Then , INNER JOIN is used to combine the table together .

GROUP BY is for grouping the aggregated result on the basis of the membership_id to get the count and last rented date for each member.


Related Solutions

Write a script to display the following patterns on the screen. Number of rows and columns...
Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop. **** **** **** a) Display the source code in an editor (#4-11) b) Execute your script in the terminal, and display the command and the result (#4-12)
1. Select all Course table data and display in primary key order. Show this query result.
Table Course:    Column    dataType Constraint ccode char(8) primary key, meetingTime char(4), room char(6), fid    decimal(3) NOT NULL, cTitle    varchar(24) NOT NULL, fee    decimal(4,2),    prereq char(8) 1. Select all Course table data and display in primary key order. Show this query result. 2. By default MySQL runs in autocommit mode. Issue a START TRANSACTION command before a series of update commands that may need to be rolled back or to be committed. ROLLBACK will "undo" commands given in the transaction. [Note:...
Problem 44 Write a query to display the employee number, last name, first name, and sum...
Problem 44 Write a query to display the employee number, last name, first name, and sum of invoice totals for all employees who completed an invoice. Sort the output by employee last name and then by first name (Partial results shown in Figure P7.44).
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through H and rows 1 through 20. Accept input in the first row of the screen. The commands are of the form column row entry, where entry is a number, a cell address preceded by a plus (e.g., +A5), a string, or a function preceded by an at sign, @. The functions are: max, min, avg, and sum. During execution of your program, build and...
The balances for the accounts listed below appeared in the Adjusted Trial Balance columns of the...
The balances for the accounts listed below appeared in the Adjusted Trial Balance columns of the work sheet. Indicate whether each balance should be extended to (a) the Income Statement columns or (b) the Balance Sheet columns.       (1)    Salaries Payable   _____ (7)    Kim Lee, Drawing   ____         (2)    Fees Earned   ______ (8)    Equipment   ____         (3)    Accounts Payable   _____ (9)    Accounts Receivable   ____         (4)    Kim Lee, Capital   ______ (10)    Accumulated Depreciation   ____         (5)    Supplies Expense   _____ (11)    Salary Expense   ____        ...
For each of the gene delivery methods listed below, write Yes or No in the spaces...
For each of the gene delivery methods listed below, write Yes or No in the spaces provided according to whether they have or do not have the features listed. Question 1 of 31 What follows is a fill in the blank question with 12 blanks. Can the delivery method be immunogenic? Retrovirus:  Blank 1. Fill in the blank, read surrounding text. Adenovirus:  Blank 2. Fill in the blank, read surrounding text. Liposome:  Blank 3. Fill in the blank, read surrounding text. Is the...
Indicate the way each of the items listed below should be reported in a balance sheet...
Indicate the way each of the items listed below should be reported in a balance sheet at December 31, 2021. asset current liability disclosure note only long term liability not reported Reporting method commercial paper noncommitted line of credit customer advances estimated quality assurance warranty cost accounts payable Long Term bonds that will be callable by the creditor in the upcoming year unless an existing violation is not corrected Note due March 3 2022 Interest accrued on note December 31,...
Suppose A2=A. On the left side A multiplies each columns of A. 1. Show that the...
Suppose A2=A. On the left side A multiplies each columns of A. 1. Show that the column space contains the eigenvectors with λ=1. 2. Show that the nullspace contains the eigenvectors with λ=0. 3.From previous two answers, you may have found the dimension of the column space and null space. Is the matrix A diagonalizable?
Any amounts that should be blank must have a zero (0)!For each employee listed below,...
Any amounts that should be blank must have a zero (0)!For each employee listed below, determine the employer’s tax liability for the payroll period, as well as the totals for each employee and for each tax. Use the following tax rates.Social Security, 6.2%, with a maximum taxable earnings of $118,500.Medicare, 1.45%, with no cut-off.State Unemployment, 5.4%, with a maximum taxable earnings of 7,000.Federal Unemployment, .6%, with a maximum taxable earnings of 7,000.Name           BeginningCumul-ativeCurrentPay           EndingCumul-ativeAmount of TaxTotal Taxes on EmployerSoc. Sec.TaxesMedi-careTaxesSUTATaxesFUTATaxesAlice116,9002,300Bob48,0002,000Cindy6,8001,080Dale127,8001,580
Please write the correct query for the following questions: Select the name of each manufacturer along...
Please write the correct query for the following questions: Select the name of each manufacturer along with the name and price of its most expensive product. Computer products are expensive. Find the maximum priced product. Which products are within $30 of the maximum priced product. Find the maker of the most expensive ‘drive’. Note, there are variety of drives in the data. Regular expressions help. Using the following table: CREATE TABLE manufacturers ( code number PRIMARY KEY NOT NULL, name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT