db_id
stringclasses 127
values | query
stringlengths 20
523
| question
stringlengths 16
224
| schema
stringclasses 127
values |
---|---|---|---|
restaurant_1
|
SELECT Rating FROM Restaurant WHERE ResName = "Subway";
|
What is the rating of the restaurant Subway?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT ResTypeName FROM Restaurant_Type;
|
List all restaurant types.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT ResTypeDescription FROM Restaurant_Type WHERE ResTypeName = "Sandwich";
|
What is the description of the restaurant type Sandwich?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT ResName , Rating FROM Restaurant ORDER BY Rating DESC LIMIT 1;
|
Which restaurants have highest rating? List the restaurant name and its rating.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Age FROM Student WHERE Fname = "Linda" AND Lname = "Smith";
|
What is the age of student Linda Smith?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Sex FROM Student WHERE Fname = "Linda" AND Lname = "Smith";
|
What is the gender of the student Linda Smith?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Fname , Lname FROM Student WHERE Major = 600;
|
List all students' first names and last names who majored in 600.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT city_code FROM Student WHERE Fname = "Linda" AND Lname = "Smith";
|
Which city does student Linda Smith live in?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT count(*) FROM Student WHERE Advisor = 1121;
|
Advisor 1121 has how many students?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Advisor , count(*) FROM Student GROUP BY Advisor ORDER BY count(Advisor) DESC LIMIT 1;
|
Which Advisor has most of students? List advisor and the number of students.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Major , count(*) FROM Student GROUP BY Major ORDER BY count(Major) ASC LIMIT 1;
|
Which major has least number of students? List the major and the number of students.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Major , count(*) FROM Student GROUP BY Major HAVING count(Major) BETWEEN 2 AND 30;
|
Which major has between 2 and 30 number of students? List major and the number of students.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Fname , Lname FROM Student WHERE Age > 18 AND Major = 600;
|
Which student's age is older than 18 and is majoring in 600? List each student's first and last name.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Fname , Lname FROM Student WHERE Age > 18 AND Major != 600 AND Sex = 'F';
|
List all female students age is older than 18 who is not majoring in 600. List students' first name and last name.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT count(*) FROM Restaurant JOIN Type_Of_Restaurant ON Restaurant.ResID = Type_Of_Restaurant.ResID JOIN Restaurant_Type ON Type_Of_Restaurant.ResTypeID = Restaurant_Type.ResTypeID GROUP BY Type_Of_Restaurant.ResTypeID HAVING Restaurant_Type.ResTypeName = 'Sandwich'
|
How many restaurant is the Sandwich type restaurant?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT sum(Spent) FROM Student JOIN Visits_Restaurant ON Student.StuID = Visits_Restaurant.StuID WHERE Student.Fname = "Linda" AND Student.Lname = "Smith";
|
How long does student Linda Smith spend on the restaurant in total?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT count(*) FROM Student JOIN Visits_Restaurant ON Student.StuID = Visits_Restaurant.StuID JOIN Restaurant ON Visits_Restaurant.ResID = Restaurant.ResID WHERE Student.Fname = "Linda" AND Student.Lname = "Smith" AND Restaurant.ResName = "Subway";
|
How many times has the student Linda Smith visited Subway?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT TIME FROM Student JOIN Visits_Restaurant ON Student.StuID = Visits_Restaurant.StuID JOIN Restaurant ON Visits_Restaurant.ResID = Restaurant.ResID WHERE Student.Fname = "Linda" AND Student.Lname = "Smith" AND Restaurant.ResName = "Subway";
|
When did Linda Smith visit Subway?
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Restaurant.ResName , sum(Visits_Restaurant.Spent) FROM Visits_Restaurant JOIN Restaurant ON Visits_Restaurant.ResID = Restaurant.ResID GROUP BY Restaurant.ResID ORDER BY sum(Visits_Restaurant.Spent) ASC LIMIT 1;
|
At which restaurant did the students spend the least amount of time? List restaurant and the time students spent on in total.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
restaurant_1
|
SELECT Student.Fname , Student.Lname FROM Student JOIN Visits_Restaurant ON Student.StuID = Visits_Restaurant.StuID GROUP BY Student.StuID ORDER BY count(*) DESC LIMIT 1;
|
Which student visited restaurant most often? List student's first name and last name.
|
create table Student (
StuID INTEGER PRIMARY KEY,
LName VARCHAR(12),
Fname VARCHAR(12),
Age INTEGER,
Sex VARCHAR(1),
Major INTEGER,
Advisor INTEGER,
city_code VARCHAR(3)
);
create table Restaurant (
ResID INTEGER PRIMARY KEY,
ResName VARCHAR(100),
Address VARCHAR(100),
Rating INTEGER
);
create table Type_Of_Restaurant (
ResID INTEGER,
ResTypeID INTEGER,
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID),
FOREIGN KEY(ResTypeID) REFERENCES Restaurant_Type(ResTypeID)
);
create table Restaurant_Type (
ResTypeID INTEGER PRIMARY KEY,
ResTypeName VARCHAR(40),
ResTypeDescription VARCHAR(100)
);
create table Visits_Restaurant (
StuID INTEGER,
ResID INTEGER,
Time TIMESTAMP,
Spent FLOAT,
FOREIGN KEY(StuID) REFERENCES Student(StuID),
FOREIGN KEY(ResID) REFERENCES Restaurant(ResID)
);
INSERT into Student values ( 1001, 'Smith', 'Linda', 18, 'F', 600, 1121,'BAL');
INSERT into Restaurant values ( 1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3);
INSERT into Restaurant_Type values ( 1, 'Sandwich', 'Simplest there is.');
INSERT into Type_Of_Restaurant values (1, 1);
INSERT into Visits_Restaurant values (1001, 1, '2017-10-09 18:15:00', 6.53);
INSERT INTO Student VALUES [(1001, 'Smith', 'Linda', 18, 'F', 600, 1121, 'BAL'), (1002, 'Kim', 'Tracy', 19, 'F', 600, 7712, 'HKG')]
INSERT INTO Restaurant VALUES [(1, 'Subway', '3233 St Paul St, Baltimore, MD 21218', 3), (2, 'Honeygrow', '3212 St Paul St, Baltimore, MD 21218', 4)]
INSERT INTO Type_Of_Restaurant VALUES [(1, 1), (2, 2)]
INSERT INTO Restaurant_Type VALUES [(1, 'Sandwich', 'Simplest there is.'), (2, 'Stir-fry', 'Classic Chinese cooking.')]
INSERT INTO Visits_Restaurant VALUES [(1001, 1, '2017-10-09 18:15:00', 6.53), (1032, 2, '2017-10-08 13:00:30', 13.2)]
|
customer_deliveries
|
SELECT actual_order_id FROM actual_orders WHERE order_status_code = 'Success'
|
Find the ids of orders whose status is 'Success'.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT t1.product_name , t1.product_price FROM products AS t1 JOIN regular_order_products AS t2 ON t1.product_id = t2.product_id GROUP BY t2.product_id ORDER BY count(*) DESC LIMIT 1
|
Find the name and price of the product that has been ordered the greatest number of times.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT count(*) FROM customers
|
Find the number of customers in total.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT count(DISTINCT payment_method) FROM customers
|
How many different payment methods are there?
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT truck_details FROM trucks ORDER BY truck_licence_number
|
Show the details of all trucks in the order of their license number.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT product_name FROM products ORDER BY product_price DESC LIMIT 1
|
Find the name of the most expensive product.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT customer_name FROM customers EXCEPT SELECT t1.customer_name FROM customers AS t1 JOIN customer_addresses AS t2 ON t1.customer_id = t2.customer_id JOIN addresses AS t3 ON t2.address_id = t3.address_id WHERE t3.state_province_county = 'California'
|
Find the names of customers who are not living in the state of California.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT customer_email , customer_name FROM customers WHERE payment_method = 'Visa'
|
List the names and emails of customers who payed by Visa card.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT t1.customer_name , t1.customer_phone FROM customers AS t1 JOIN customer_addresses AS t2 ON t1.customer_id = t2.customer_id JOIN addresses AS t3 ON t2.address_id = t3.address_id WHERE t3.state_province_county = 'California'
|
Find the names and phone numbers of customers living in California state.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT state_province_county FROM addresses WHERE address_id NOT IN (SELECT employee_address_id FROM Employees)
|
Find the states which do not have any employee in their record.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT customer_name , customer_phone , customer_email FROM Customers ORDER BY date_became_customer
|
List the names, phone numbers, and emails of all customers sorted by their dates of becoming customers.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT customer_name FROM Customers ORDER BY date_became_customer LIMIT 5
|
Find the name of the first 5 customers.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT payment_method FROM Customers GROUP BY payment_method ORDER BY count(*) DESC LIMIT 1
|
Find the payment method that is used most frequently.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT route_name FROM Delivery_Routes ORDER BY route_name
|
List the names of all routes in alphabetic order.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT t1.route_name FROM Delivery_Routes AS t1 JOIN Delivery_Route_Locations AS t2 ON t1.route_id = t2.route_id GROUP BY t1.route_id ORDER BY count(*) DESC LIMIT 1
|
Find the name of route that has the highest number of deliveries.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
customer_deliveries
|
SELECT t2.state_province_county , count(*) FROM customer_addresses AS t1 JOIN addresses AS t2 ON t1.address_id = t2.address_id GROUP BY t2.state_province_county
|
List the state names and the number of customers living in each state.
|
CREATE TABLE `Products` (
`product_id` INTEGER PRIMARY KEY,
`product_name` VARCHAR(20),
`product_price` DECIMAL(19,4),
`product_description` VARCHAR(255)
);
CREATE TABLE `Addresses` (
`address_id` INTEGER PRIMARY KEY,
`address_details` VARCHAR(80),
`city` VARCHAR(50),
`zip_postcode` VARCHAR(20),
`state_province_county` VARCHAR(50),
`country` VARCHAR(50)
);
CREATE TABLE `Customers` (
`customer_id` INTEGER PRIMARY KEY,
`payment_method` VARCHAR(10) NOT NULL,
`customer_name` VARCHAR(80),
`customer_phone` VARCHAR(80),
`customer_email` VARCHAR(80),
`date_became_customer` DATETIME
);
CREATE TABLE `Regular_Orders` (
`regular_order_id` INTEGER PRIMARY KEY,
`distributer_id` INTEGER NOT NULL,
FOREIGN KEY (`distributer_id` ) REFERENCES `Customers`(`customer_id` )
);
CREATE TABLE `Regular_Order_Products` (
`regular_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Orders` (
`actual_order_id` INTEGER PRIMARY KEY,
`order_status_code` VARCHAR(10) NOT NULL,
`regular_order_id` INTEGER NOT NULL,
`actual_order_date` DATETIME,
FOREIGN KEY (`regular_order_id` ) REFERENCES `Regular_Orders`(`regular_order_id` )
);
CREATE TABLE `Actual_Order_Products` (
`actual_order_id` INTEGER NOT NULL,
`product_id` INTEGER NOT NULL,
FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` )
);
CREATE TABLE `Customer_Addresses` (
`customer_id` INTEGER NOT NULL,
`address_id` INTEGER NOT NULL,
`date_from` DATETIME NOT NULL,
`address_type` VARCHAR(10) NOT NULL,
`date_to` DATETIME,
FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ),
FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Delivery_Routes` (
`route_id` INTEGER PRIMARY KEY,
`route_name` VARCHAR(50),
`other_route_details` VARCHAR(255)
);
CREATE TABLE `Delivery_Route_Locations` (
`location_code` VARCHAR(10) PRIMARY KEY,
`route_id` INTEGER NOT NULL,
`location_address_id` INTEGER NOT NULL,
`location_name` VARCHAR(50),
FOREIGN KEY (`location_address_id` ) REFERENCES `Addresses`(`address_id` ),
FOREIGN KEY (`route_id` ) REFERENCES `Delivery_Routes`(`route_id` )
);
CREATE TABLE `Trucks` (
`truck_id` INTEGER PRIMARY KEY,
`truck_licence_number` VARCHAR(20),
`truck_details` VARCHAR(255)
);
CREATE TABLE `Employees` (
`employee_id` INTEGER PRIMARY KEY,
`employee_address_id` INTEGER NOT NULL,
`employee_name` VARCHAR(80),
`employee_phone` VARCHAR(80),
FOREIGN KEY (`employee_address_id` ) REFERENCES `Addresses`(`address_id` )
);
CREATE TABLE `Order_Deliveries` (
`location_code` VARCHAR(10) NOT NULL,
`actual_order_id` INTEGER NOT NULL,
`delivery_status_code` VARCHAR(10) NOT NULL,
`driver_employee_id` INTEGER NOT NULL,
`truck_id` INTEGER NOT NULL,
`delivery_date` DATETIME,
FOREIGN KEY (`truck_id` ) REFERENCES `Trucks`(`truck_id` ),
FOREIGN KEY (`actual_order_id` ) REFERENCES `Actual_Orders`(`actual_order_id` ),
FOREIGN KEY (`location_code` ) REFERENCES `Delivery_Route_Locations`(`location_code` ),
FOREIGN KEY (`driver_employee_id` ) REFERENCES `Employees`(`employee_id` )
);
INSERT INTO Products (`product_id`, `product_name`, `product_price`, `product_description`) VALUES (1, 'dvds', '1322.7800', 'good condition')
INSERT INTO Addresses (`address_id`, `address_details`, `city`, `zip_postcode`, `state_province_county`, `country`) VALUES (1, '92283 Lora Forges Suite 322', 'Mohrville', '271', 'Nebraska', 'USA')
INSERT INTO Customers (`customer_id`, `payment_method`, `customer_name`, `customer_phone`, `customer_email`, `date_became_customer`) VALUES (1, 'Visa', 'Ron Emard', '1-382-503-5179x53639', '[email protected]', '2011-04-25 22:20:35')
INSERT INTO Regular_Orders (`regular_order_id`, `distributer_id`) VALUES (1, 12)
INSERT INTO Regular_Order_Products (`regular_order_id`, `product_id`) VALUES (5, 3)
INSERT INTO Actual_Orders (`actual_order_id`, `order_status_code`, `regular_order_id`, `actual_order_date`) VALUES (1, 'Success', 8, '2018-03-02 23:26:19')
INSERT INTO Actual_Order_Products (`actual_order_id`, `product_id`) VALUES (2, 1)
INSERT INTO Customer_Addresses (`customer_id`, `address_id`, `date_from`, `address_type`, `date_to`) VALUES (5, 6, '2016-09-06 19:23:46', 'House', '2018-02-25 15:34:58')
INSERT INTO Delivery_Routes (`route_id`, `route_name`, `other_route_details`) VALUES (1, 'Torphy Ltd', '16893 Wilderman Terrace
INSERT INTO Delivery_Route_Locations (`location_code`, `route_id`, `location_address_id`, `location_name`) VALUES ('27 City Rd', 11, 5, 'Labadie-Crooks')
INSERT INTO Trucks (`truck_id`, `truck_licence_number`, `truck_details`) VALUES (1, '58110', 'Frida')
INSERT INTO Employees (`employee_id`, `employee_address_id`, `employee_name`, `employee_phone`) VALUES (1, 4, 'Kacie', '716-650-2081')
INSERT INTO Order_Deliveries (`location_code`, `actual_order_id`, `delivery_status_code`, `driver_employee_id`, `truck_id`, `delivery_date`) VALUES ('27 City Rd', 11, 'Ready', 6, 11, '2018-03-21 00:57:22')
|
sakila_1
|
SELECT count(DISTINCT last_name) FROM actor
|
How many different last names do the actors and actresses have?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(DISTINCT last_name) FROM actor
|
Count the number of different last names actors have.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT first_name FROM actor GROUP BY first_name ORDER BY count(*) DESC LIMIT 1
|
What is the most popular first name of the actors?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT first_name FROM actor GROUP BY first_name ORDER BY count(*) DESC LIMIT 1
|
Return the most common first name among all actors.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT first_name , last_name FROM actor GROUP BY first_name , last_name ORDER BY count(*) DESC LIMIT 1
|
What is the most popular full name of the actors?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT first_name , last_name FROM actor GROUP BY first_name , last_name ORDER BY count(*) DESC LIMIT 1
|
Return the most common full name among all actors.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT district FROM address GROUP BY district HAVING count(*) >= 2
|
Which districts have at least two addresses?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT district FROM address GROUP BY district HAVING count(*) >= 2
|
Give the districts which have two or more addresses.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT phone , postal_code FROM address WHERE address = '1031 Daugavpils Parkway'
|
What is the phone number and postal code of the address 1031 Daugavpils Parkway?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT phone , postal_code FROM address WHERE address = '1031 Daugavpils Parkway'
|
Give the phone and postal code corresponding to the address '1031 Daugavpils Parkway'.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.city , count(*) , T1.city_id FROM address AS T1 JOIN city AS T2 ON T1.city_id = T2.city_id GROUP BY T1.city_id ORDER BY count(*) DESC LIMIT 1
|
Which city has the most addresses? List the city name, number of addresses, and city id.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.city , count(*) , T1.city_id FROM address AS T1 JOIN city AS T2 ON T1.city_id = T2.city_id GROUP BY T1.city_id ORDER BY count(*) DESC LIMIT 1
|
What are the city name, id, and number of addresses corresponding to the city with the most addressed?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM address WHERE district = 'California'
|
How many addresses are in the district of California?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM address WHERE district = 'California'
|
Count the number of addressed in the California district.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title , film_id FROM film WHERE rental_rate = 0.99 INTERSECT SELECT T1.title , T1.film_id FROM film AS T1 JOIN inventory AS T2 ON T1.film_id = T2.film_id GROUP BY T1.film_id HAVING count(*) < 3
|
Which film is rented at a fee of 0.99 and has less than 3 in the inventory? List the film title and id.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title , film_id FROM film WHERE rental_rate = 0.99 INTERSECT SELECT T1.title , T1.film_id FROM film AS T1 JOIN inventory AS T2 ON T1.film_id = T2.film_id GROUP BY T1.film_id HAVING count(*) < 3
|
What are the title and id of the film which has a rental rate of 0.99 and an inventory of below 3?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM city AS T1 JOIN country AS T2 ON T1.country_id = T2.country_id WHERE T2.country = 'Australia'
|
How many cities are in Australia?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM city AS T1 JOIN country AS T2 ON T1.country_id = T2.country_id WHERE T2.country = 'Australia'
|
Count the number of cities in Australia.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.country FROM city AS T1 JOIN country AS T2 ON T1.country_id = T2.country_id GROUP BY T2.country_id HAVING count(*) >= 3
|
Which countries have at least 3 cities?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.country FROM city AS T1 JOIN country AS T2 ON T1.country_id = T2.country_id GROUP BY T2.country_id HAVING count(*) >= 3
|
What are the countries that contain 3 or more cities?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT payment_date FROM payment WHERE amount > 10 UNION SELECT T1.payment_date FROM payment AS T1 JOIN staff AS T2 ON T1.staff_id = T2.staff_id WHERE T2.first_name = 'Elsa'
|
Find all the payment dates for the payments with an amount larger than 10 and the payments handled by a staff person with the first name Elsa.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT payment_date FROM payment WHERE amount > 10 UNION SELECT T1.payment_date FROM payment AS T1 JOIN staff AS T2 ON T1.staff_id = T2.staff_id WHERE T2.first_name = 'Elsa'
|
What are the payment dates for any payments that have an amount greater than 10 or were handled by a staff member with the first name Elsa?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM customer WHERE active = '1'
|
How many customers have an active value of 1?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM customer WHERE active = '1'
|
Count the number of customers who are active.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title , rental_rate FROM film ORDER BY rental_rate DESC LIMIT 1
|
Which film has the highest rental rate? And what is the rate?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title , rental_rate FROM film ORDER BY rental_rate DESC LIMIT 1
|
What are the title and rental rate of the film with the highest rental rate?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.title , T2.film_id , T2.description FROM film_actor AS T1 JOIN film AS T2 ON T1.film_id = T2.film_id GROUP BY T2.film_id ORDER BY count(*) DESC LIMIT 1
|
Which film has the most number of actors or actresses? List the film name, film id and description.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.title , T2.film_id , T2.description FROM film_actor AS T1 JOIN film AS T2 ON T1.film_id = T2.film_id GROUP BY T2.film_id ORDER BY count(*) DESC LIMIT 1
|
What are the title, id, and description of the movie with the greatest number of actors?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.first_name , T2.last_name , T2.actor_id FROM film_actor AS T1 JOIN actor AS T2 ON T1.actor_id = T2.actor_id GROUP BY T2.actor_id ORDER BY count(*) DESC LIMIT 1
|
Which film actor (actress) starred the most films? List his or her first name, last name and actor id.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.first_name , T2.last_name , T2.actor_id FROM film_actor AS T1 JOIN actor AS T2 ON T1.actor_id = T2.actor_id GROUP BY T2.actor_id ORDER BY count(*) DESC LIMIT 1
|
Return the full name and id of the actor or actress who starred in the greatest number of films.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.first_name , T2.last_name FROM film_actor AS T1 JOIN actor AS T2 ON T1.actor_id = T2.actor_id GROUP BY T2.actor_id HAVING count(*) > 30
|
Which film actors (actresses) played a role in more than 30 films? List his or her first name and last name.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.first_name , T2.last_name FROM film_actor AS T1 JOIN actor AS T2 ON T1.actor_id = T2.actor_id GROUP BY T2.actor_id HAVING count(*) > 30
|
What are the full names of actors who had roles in more than 30 films?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT store_id FROM inventory GROUP BY store_id ORDER BY count(*) DESC LIMIT 1
|
Which store owns most items?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT store_id FROM inventory GROUP BY store_id ORDER BY count(*) DESC LIMIT 1
|
What is the id of the store that has the most items in inventory?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT sum(amount) FROM payment
|
What is the total amount of all payments?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT sum(amount) FROM payment
|
Return the sum of all payment amounts.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.first_name , T1.last_name , T1.customer_id FROM customer AS T1 JOIN payment AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY sum(amount) ASC LIMIT 1
|
Which customer, who has made at least one payment, has spent the least money? List his or her first name, last name, and the id.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.first_name , T1.last_name , T1.customer_id FROM customer AS T1 JOIN payment AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY sum(amount) ASC LIMIT 1
|
What is the full name and id of the customer who has the lowest total amount of payment?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.name FROM category AS T1 JOIN film_category AS T2 ON T1.category_id = T2.category_id JOIN film AS T3 ON T2.film_id = T3.film_id WHERE T3.title = 'HUNGER ROOF'
|
What is the genre name of the film HUNGER ROOF?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.name FROM category AS T1 JOIN film_category AS T2 ON T1.category_id = T2.category_id JOIN film AS T3 ON T2.film_id = T3.film_id WHERE T3.title = 'HUNGER ROOF'
|
Return the name of the category to which the film 'HUNGER ROOF' belongs.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.name , T1.category_id , count(*) FROM film_category AS T1 JOIN category AS T2 ON T1.category_id = T2.category_id GROUP BY T1.category_id
|
How many films are there in each category? List the genre name, genre id and the count.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.name , T1.category_id , count(*) FROM film_category AS T1 JOIN category AS T2 ON T1.category_id = T2.category_id GROUP BY T1.category_id
|
What are the names and ids of the different categories, and how many films are in each?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.title , T1.film_id FROM film AS T1 JOIN inventory AS T2 ON T1.film_id = T2.film_id GROUP BY T1.film_id ORDER BY count(*) DESC LIMIT 1
|
Which film has the most copies in the inventory? List both title and id.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.title , T1.film_id FROM film AS T1 JOIN inventory AS T2 ON T1.film_id = T2.film_id GROUP BY T1.film_id ORDER BY count(*) DESC LIMIT 1
|
What is the title and id of the film that has the greatest number of copies in inventory?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.title , T2.inventory_id FROM film AS T1 JOIN inventory AS T2 ON T1.film_id = T2.film_id JOIN rental AS T3 ON T2.inventory_id = T3.inventory_id GROUP BY T2.inventory_id ORDER BY count(*) DESC LIMIT 1
|
What is the film title and inventory id of the item in the inventory which was rented most frequently?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.title , T2.inventory_id FROM film AS T1 JOIN inventory AS T2 ON T1.film_id = T2.film_id JOIN rental AS T3 ON T2.inventory_id = T3.inventory_id GROUP BY T2.inventory_id ORDER BY count(*) DESC LIMIT 1
|
Return the title and inventory id of the film that is rented most often.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(DISTINCT language_id) FROM film
|
How many languages are in these films?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(DISTINCT language_id) FROM film
|
Count the number of different languages in these films.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title FROM film WHERE rating = 'R'
|
What are all the movies rated as R? List the titles.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title FROM film WHERE rating = 'R'
|
Return the titles of any movies with an R rating.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.address FROM store AS T1 JOIN address AS T2 ON T1.address_id = T2.address_id WHERE store_id = 1
|
Where is store 1 located?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.address FROM store AS T1 JOIN address AS T2 ON T1.address_id = T2.address_id WHERE store_id = 1
|
Return the address of store 1.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.first_name , T1.last_name , T1.staff_id FROM staff AS T1 JOIN payment AS T2 ON T1.staff_id = T2.staff_id GROUP BY T1.staff_id ORDER BY count(*) ASC LIMIT 1
|
Which staff handled least number of payments? List the full name and the id.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T1.first_name , T1.last_name , T1.staff_id FROM staff AS T1 JOIN payment AS T2 ON T1.staff_id = T2.staff_id GROUP BY T1.staff_id ORDER BY count(*) ASC LIMIT 1
|
Give the full name and staff id of the staff who has handled the fewest payments.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.name FROM film AS T1 JOIN LANGUAGE AS T2 ON T1.language_id = T2.language_id WHERE T1.title = 'AIRPORT POLLOCK'
|
Which language does the film AIRPORT POLLOCK use? List the language name.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT T2.name FROM film AS T1 JOIN LANGUAGE AS T2 ON T1.language_id = T2.language_id WHERE T1.title = 'AIRPORT POLLOCK'
|
What is the name of the language that the film 'AIRPORT POLLOCK' is in?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM store
|
How many stores are there?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM store
|
Count the number of stores.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(DISTINCT rating) FROM film
|
How many kinds of different ratings are listed?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(DISTINCT rating) FROM film
|
Count the number of different film ratings.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title FROM film WHERE special_features LIKE '%Deleted Scenes%'
|
Which movies have 'Deleted Scenes' as a substring in the special feature?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT title FROM film WHERE special_features LIKE '%Deleted Scenes%'
|
Return the titles of films that include 'Deleted Scenes' in their special feature section.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM inventory WHERE store_id = 1
|
How many items in inventory does store 1 have?
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
sakila_1
|
SELECT count(*) FROM inventory WHERE store_id = 1
|
Count the number of items store 1 has in stock.
|
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
);
CREATE TABLE address (
address_id SMALLINT UNSIGNED NOT NULL,
address VARCHAR(50) NOT NULL,
address2 VARCHAR(50) DEFAULT NULL,
district VARCHAR(20) NOT NULL,
city_id SMALLINT UNSIGNED NOT NULL,
postal_code VARCHAR(10) DEFAULT NULL,
phone VARCHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (address_id),
FOREIGN KEY (city_id) REFERENCES city (city_id)
);
CREATE TABLE category (
category_id TINYINT UNSIGNED NOT NULL,
name VARCHAR(25) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (category_id)
);
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
FOREIGN KEY (country_id) REFERENCES country (country_id)
);
CREATE TABLE country (
country_id SMALLINT UNSIGNED NOT NULL,
country VARCHAR(50) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)
);
CREATE TABLE customer (
customer_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
email VARCHAR(50) DEFAULT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (customer_id),
FOREIGN KEY (address_id) REFERENCES address (address_id),
FOREIGN KEY (store_id) REFERENCES store (store_id)
);
CREATE TABLE film (
film_id SMALLINT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
release_year YEAR DEFAULT NULL,
language_id TINYINT UNSIGNED NOT NULL,
original_language_id TINYINT UNSIGNED DEFAULT NULL,
rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
length SMALLINT UNSIGNED DEFAULT NULL,
replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
rating DEFAULT 'G',
special_features DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id),
FOREIGN KEY (language_id) REFERENCES language (language_id),
FOREIGN KEY (original_language_id) REFERENCES language (language_id)
);
CREATE TABLE film_actor (
actor_id SMALLINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id,film_id),
FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE film_category (
film_id SMALLINT UNSIGNED NOT NULL,
category_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (film_id, category_id),
FOREIGN KEY (film_id) REFERENCES film (film_id),
FOREIGN KEY (category_id) REFERENCES category (category_id)
);
CREATE TABLE film_text (
film_id SMALLINT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (film_id)
);
CREATE TABLE inventory (
inventory_id MEDIUMINT UNSIGNED NOT NULL,
film_id SMALLINT UNSIGNED NOT NULL,
store_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (inventory_id),
FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (film_id) REFERENCES film (film_id)
);
CREATE TABLE language (
language_id TINYINT UNSIGNED NOT NULL,
name CHAR(20) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (language_id)
);
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
rental_id INT DEFAULT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id),
FOREIGN KEY (rental_id) REFERENCES rental (rental_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
);
CREATE TABLE rental (
rental_id INT NOT NULL,
rental_date DATETIME NOT NULL,
inventory_id MEDIUMINT UNSIGNED NOT NULL,
customer_id SMALLINT UNSIGNED NOT NULL,
return_date DATETIME DEFAULT NULL,
staff_id TINYINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rental_id),
FOREIGN KEY (staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE staff (
staff_id TINYINT UNSIGNED NOT NULL,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
picture BLOB DEFAULT NULL,
email VARCHAR(50) DEFAULT NULL,
store_id TINYINT UNSIGNED NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
username VARCHAR(16) NOT NULL,
password VARCHAR(40) DEFAULT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (staff_id),
--FOREIGN KEY (store_id) REFERENCES store (store_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
CREATE TABLE store (
store_id TINYINT UNSIGNED NOT NULL,
manager_staff_id TINYINT UNSIGNED NOT NULL,
address_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (store_id),
FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id),
FOREIGN KEY (address_id) REFERENCES address (address_id)
);
INSERT INTO actor VALUES (1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),(2,'NICK','WAHLBERG','2006-02-15 04:34:33')
INSERT INTO address VALUES (1,'47 MySakila Drive',NULL,'Alberta',300,'','','2006-02-15 04:45:30'),(2,'28 MySQL Boulevard',NULL,'QLD',576,'','','2006-02-15 04:45:30')
INSERT INTO category VALUES (1,'Action','2006-02-15 04:46:27'),(2,'Animation','2006-02-15 04:46:27')
INSERT INTO city VALUES (1,'A Corua (La Corua)',87,'2006-02-15 04:45:25')
INSERT INTO country VALUES (1,'Afghanistan','2006-02-15 04:44:00'),(2,'Algeria','2006-02-15 04:44:00')
INSERT INTO customer VALUES (1,1,'MARY','SMITH','[email protected]',5,1,'2006-02-14 22:04:36','2006-02-15 04:57:20'),(2,1,'PATRICIA','JOHNSON','[email protected]',6,1,'2006-02-14 22:04:36','2006-02-15 04:57:20')
INSERT INTO film VALUES (1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies',2006,1,NULL,6,'0.99',86,'20.99','PG','Deleted Scenes,Behind the Scenes','2006-02-15 05:03:42'),(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China',2006,1,NULL,3,'4.99',48,'12.99','G','Trailers,Deleted Scenes','2006-02-15 05:03:42')
INSERT INTO film_actor VALUES (1,1,'2006-02-15 05:05:03'),(1,23,'2006-02-15 05:05:03')
INSERT INTO film_category VALUES (1,6,'2006-02-15 05:07:09'),(2,11,'2006-02-15 05:07:09')
INSERT INTO inventory VALUES (1,1,1,'2006-02-15 05:09:17'),(2,1,1,'2006-02-15 05:09:17')
INSERT INTO payment VALUES (1,1,1,76,'2.99','2005-05-25 11:30:37','2006-02-15 22:12:30'),(2,1,1,573,'0.99','2005-05-28 10:35:23','2006-02-15 22:12:30')
INSERT INTO rental VALUES (1,'2005-05-24 22:53:30',367,130,'2005-05-26 22:04:30',1,'2006-02-15 21:30:53'),(2,'2005-05-24 22:54:33',1525,459,'2005-05-28 19:40:33',1,'2006-02-15 21:30:53')
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.