Dludora/DAIL-BIRD huggingface dataset:
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many cars on a train that runs in the east direction have a flat roof? flat roof refers to roof = 'flat' */
SELECT 
SELECT SUM(CASE WHEN T1.roof = 'flat' THEN 1 ELSE 0 END)as count FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T2.direction = 'east'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Among the cars on a train that runs in the east direction, how many of them have a flat roof and a circle load shape? flat roof refers to roof = 'flat'; load_shape = 'circle' */
SELECT 
SELECT SUM(CASE WHEN T1.load_shape = 'circle' THEN 1 ELSE 0 END)as count FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T2.direction = 'east' AND T1.roof = 'flat'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Trains that run in which direction have more rectangle-shaped cars in total? more rectangle-shaped cars refers to MAX(rectCarsNum) */
SELECT 
SELECT T1.direction FROM trains AS T1 INNER JOIN ( SELECT train_id, COUNT(id) AS rectCarsNum FROM cars WHERE shape = 'rectangle' GROUP BY train_id ) AS T2 ON T1.id = T2.train_id ORDER BY T2.rectCarsNum DESC
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Please list the directions in which the trains with 4 short cars run. short refers to len = 'short'; 4 cars run refers to position = 4 */
SELECT 
SELECT T2.direction FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.len = 'short' AND T1.position = 4
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: What is the average number of cars on trains that run in the east direction? calculation = DIVIDE(count(id), count(train_id)) */
SELECT 
SELECT CAST(COUNT(T1.id) AS REAL) / COUNT(DISTINCT T1.train_id) FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T2.direction = 'east'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Among the trains that have at least one non-regular shaped car, what is the percentage of it running in the east direction? non-regular shaped car refers to shape in ('bucket', 'ellipse'); calculation = MULTIPLY(DIVIDE(count(direction = 'east' then train_id)), count(train_id), 100) */
SELECT 
SELECT CAST(COUNT(DISTINCT CASE WHEN T2.direction = 'east' THEN T1.train_id ELSE NULL END) AS REAL) * 100 / COUNT(DISTINCT T1.train_id) FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.shape IN ('bucket', 'ellipse')
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many short cars are in the shape of hexagon? short cars refers to len = 'short'; in the shape of hexagon refers to shape = 'hexagon' */
SELECT 
SELECT COUNT(id) FROM cars WHERE shape = 'hexagon' AND len = 'short'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many trains are running west? west is a direction */
SELECT 
SELECT COUNT(id) FROM trains WHERE direction = 'west'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: What are the load shapes of all the short ellipse cars? short refers to len = 'short'; ellipse cars refers to shape = 'ellipse' */
SELECT 
SELECT load_shape FROM cars WHERE shape = 'ellipse' AND len = 'short'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: What are the ids of the train running east? east is a direction */
SELECT 
SELECT id FROM trains WHERE direction = 'east'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many wheels do the long cars have? long cars refers to len = 'long' */
SELECT 
SELECT SUM(wheels) FROM cars WHERE len = 'long'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Which direction do the majority of the trains are running? majority of train refers to MAX(count(id)) */
SELECT 
SELECT direction FROM trains GROUP BY direction ORDER BY COUNT(id) DESC
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Among the trains running east, how many trains have at least 4 cars? east is a direction; at least 4 cars refers to carsNum > = 4 */
SELECT 
SELECT SUM(CASE WHEN T1.direction = 'east' THEN 1 ELSE 0 END)as count FROM trains AS T1 INNER JOIN ( SELECT train_id, COUNT(id) AS carsNum FROM cars GROUP BY train_id ) AS T2 ON T1.id = T2.train_id WHERE T2.carsNum >= 4
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Which direction do most of the trains with rectangle-shaped second cars run? most of the trains refers to MAX(count(id)); second cars refers to position = 2 */
SELECT 
SELECT T2.direction FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.position = 2 AND T1.shape = 'rectangle' GROUP BY T2.direction ORDER BY COUNT(T2.id) DESC LIMIT 1
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many trains running west have double sided cars in 3rd position? west is a direction; double sided cars refers to sides = 'double'; 3rd position refers to position = 3 */
SELECT 
SELECT COUNT(T.train_id) FROM (SELECT T1.train_id FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.position = 3 AND T2.direction = 'west' AND T1.sides = 'double' GROUP BY T1.train_id)as T
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many eastbound trains have rectangular-shaped head cars? eastbound refers to direction = 'east'; head cars refers to position = 1 */
SELECT 
SELECT COUNT(T.train_id) FROM (SELECT T1.train_id FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.position = 1 AND T2.direction = 'east' AND T1.shape = 'rectangle' GROUP BY T1.train_id)as T
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Among the trains running west, how many trains have no more than one car with an open roof? running west refers to direction = 'west'; open roof refers to roof = 'none' */
SELECT 
SELECT SUM(CASE WHEN T1.direction = 'west' THEN 1 ELSE 0 END)as count FROM trains AS T1 INNER JOIN ( SELECT train_id, COUNT(id) FROM cars WHERE roof = 'none' GROUP BY train_id HAVING COUNT(id) = 1 ) AS T2 ON T1.id = T2.train_id
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Which direction does the majority of the trains that have 3 cars are running? 3 cars refers to carsNum = 3 */
SELECT 
SELECT T1.direction FROM trains AS T1 INNER JOIN ( SELECT train_id, COUNT(id) AS carsNum FROM cars GROUP BY train_id HAVING carsNum = 3 ) AS T2 ON T1.id = T2.train_id GROUP BY T1.direction
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many trains with fully loaded head cars are running east? fully loaded refers to load_num = 3; head cars refers to position = 1 */
SELECT 
SELECT COUNT(DISTINCT T1.train_id) FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.position = 1 AND T1.load_num = 3
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: How many cars running east have double-sided tail cars? east is an direction; double-sided refers to sides = 'double'; tail refers to carsposition = trailPosi */
SELECT 
SELECT COUNT(T1.id) FROM trains AS T1 INNER JOIN cars AS T2 ON T1.id = T2.train_id INNER JOIN ( SELECT train_id, MAX(position) AS trailPosi FROM cars GROUP BY train_id ) AS T3 ON T1.id = T3.train_id WHERE T1.direction = 'east' AND T2.position = T3.trailPosi AND T2.sides = 'double'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: List all the directions of the trains that have empty cars. empty cars refers to load_num = 0 */
SELECT 
SELECT T2.direction FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.load_num = 0
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: What is the direction of the train with a diamond-shaped load in its 2nd car? 2nd car refers to position = 2 */
SELECT 
SELECT T2.direction FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.position = 2 AND T1.shape = 'diamond'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Among the trains running west, how many trains have three-wheeled, jagged roof cars? west is an direction; three-wheeled refers to wheels = 3; jagged roof refers to roof = 'jagged' */
SELECT 
SELECT SUM(CASE WHEN T2.direction = 'west' THEN 1 ELSE 0 END)as count FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.wheels = 3 AND T1.roof = 'jagged'
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: Provide the directions for all the trains that have 2 or less cars. 2 or less cars refers to trailPosi < = 2 */
SELECT 
SELECT T1.direction FROM trains AS T1 INNER JOIN ( SELECT train_id, MAX(position) AS trailPosi FROM cars GROUP BY train_id ) AS T2 ON T1.id = T2.train_id WHERE T2.trailPosi <= 2
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: What is the percentage of all the trains with at least 4 cars? List the directions of the said trains. at least 4 cars refers to trailPosi > = 4; calculation = MULTIPLY(DIVIDE(count(trailPosi > = 4 then id), count(id)), 100) */
SELECT 
SELECT CAST(COUNT(CASE WHEN T2.trailPosi >= 4 THEN T1.id ELSE NULL END) AS REAL) * 100 / COUNT(T1.id) FROM trains AS T1 INNER JOIN ( SELECT train_id, MAX(position) AS trailPosi FROM cars GROUP BY train_id ) AS T2 ON T1.id = T2.train_id UNION ALL SELECT T1.direction FROM trains AS T1 INNER JOIN ( SELECT train_id, MAX(position) AS trailPosi FROM cars t GROUP BY train_id ) AS T2 ON T1.id = T2.train_id AND T2.trailPosi >= 4
/* Given the following database schema: */
CREATE TABLE `cars` (
  `id` INTEGER NOT NULL,
  `train_id` INTEGER DEFAULT NULL,
  `position` INTEGER DEFAULT NULL,
  `shape` TEXT DEFAULT NULL,
  `len`TEXT DEFAULT NULL,
  `sides` TEXT DEFAULT NULL,
  `roof` TEXT DEFAULT NULL,
  `wheels` INTEGER DEFAULT NULL,
  `load_shape` TEXT DEFAULT NULL,
  `load_num` INTEGER DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`train_id`) REFERENCES `trains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE `trains` (
  `id` INTEGER NOT NULL,
  `direction` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

/* Answer the following: List all the load shapes of all head cars of each train and identify which load shape has the highest number. Calculate the percentage of the trains with the said head car that are running eas which load shape has the highest number refers to MAX(load_shape); head car refers to position = 1; east is a direction; calculation = MULTIPLY(DIVIDE(count(direction = 'east' where MAX(load_shape) where position = 1 then id), count(id)), 100) */
SELECT 
SELECT DISTINCT T3.load_shape FROM ( SELECT load_shape, train_id FROM cars WHERE position = 1 ORDER BY train_id DESC ) AS T3 UNION ALL SELECT T4.load_shape FROM ( SELECT load_shape, train_id FROM cars WHERE position = 1 ORDER BY train_id DESC LIMIT 1 ) AS T4 UNION ALL SELECT (CAST(COUNT(DISTINCT CASE WHEN T2.direction = 'east' THEN T2.id ELSE NULL END) AS REAL) * 100 / COUNT(DISTINCT T2.id)) FROM cars AS T1 INNER JOIN trains AS T2 ON T1.train_id = T2.id WHERE T1.position = 1 AND T1.load_shape = ( SELECT T4.load_shape FROM ( SELECT load_shape, train_id FROM cars AS T WHERE position = 1 ORDER BY train_id DESC LIMIT 1 ) AS T4 )
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Please list the names of the characters in the movie Look Who's Talking. movie Look Who's Talking refers to title = 'Look Who's Talking' */
SELECT 
SELECT T2.`Character Name` FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID WHERE T1.Title = 'Look Who''s Talking'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Which character has the longest screen time in the movie Batman? longest screen time refers to max(screentime); movie Batman refers to title = 'Batman' */
SELECT 
SELECT T2.`Character Name` FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID WHERE T1.Title = 'Batman' ORDER BY T2.screentime DESC LIMIT 1
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Which actor played the role of Joker in the movie Batman? role of Joker refers to character_name = 'Joker'; movie Batman refers to title = 'Batman' */
SELECT 
SELECT T3.Name FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T1.Title = 'Batman' AND T2.`Character Name` = 'Joker'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Please list the names of the actors who played a role in the movie Batman. movie Batman refers to title = 'Batman' */
SELECT 
SELECT T3.Name FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T1.Title = 'Batman'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Which movie is the character Dr. Archibald 'Moonlight' Graham from? movie name refers to title; character Dr. Archibald 'Moonlight' Graham refers to character_name = 'Dr. Archibald 'Moonlight' Graham' */
SELECT 
SELECT T1.Title FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID WHERE T2.`Character Name` = 'Dr. Archibald ''Moonlight'' Graham'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Please list the names of the movies starring Tom Cruise. movie name refers to title; starring Tom Cruise refers to name = 'Tom Cruise' */
SELECT 
SELECT T1.Title FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T3.Name = 'Tom Cruise'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: How many movies starring Morgan Freeman are suggested by parental guidance? 'suggested by parental guidance' refers to mpaa_rating = 'PG' */
SELECT 
SELECT COUNT(*) FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T3.Name = 'Morgan Freeman' AND T1.`MPAA Rating` = 'PG'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Among the movies starring Tom Cruise, which one of them has the best quality? starring Tom Cruise refers to name = 'Tom Cruise'; best quality refers to max(rating) */
SELECT 
SELECT T1.Title FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T3.Name = 'Tom Cruise' ORDER BY T1.Rating DESC LIMIT 1
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: What is the name of the character played by Tom Cruise in the movie Born on the Fourth of July? played by Tom Cruise refers to name = 'Tom Cruise'; movie Born on the Fourth of July refers to title = 'Born on the Fourth of July' */
SELECT 
SELECT T2.`Character Name` FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T3.Name = 'Tom Cruise' AND T1.Title = 'Born on the Fourth of July'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Please list the names of all the characters played by Tom Cruise. played by Tom Cruise refers to name = 'Tom Cruise' */
SELECT 
SELECT T1.`Character Name` FROM characters AS T1 INNER JOIN actor AS T2 ON T1.ActorID = T2.ActorID WHERE T2.Name = 'Tom Cruise'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Among the actors who starred in the movie Batman, which one of them is the tallest? movie Batman refers to title = 'Batman'; tallest refers to max(height_inches) */
SELECT 
SELECT T3.Name FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T1.Title = 'Batman' ORDER BY T3.`Height (Inches)` DESC LIMIT 1
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: How many movies star a male African American actor? male refers to gender = 'Male'; African American refers to ethnicity = 'African American' */
SELECT 
SELECT COUNT(*) FROM characters AS T1 INNER JOIN actor AS T2 ON T1.ActorID = T2.ActorID WHERE T2.Gender = 'Male' AND T2.Ethnicity = 'African American'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: What is the average rating of all the movies starring Tom Cruise? starring Tom Cruise refers to name = 'Tom Cruise'; average rating = divide(sum(rating where name = 'Tom Cruise'), count(movieid where name = 'Tom Cruise')) */
SELECT 
SELECT AVG(T1.Rating) FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID INNER JOIN actor AS T3 ON T3.ActorID = T2.ActorID WHERE T3.Name = 'Tom Cruise'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: How much longer in percentage is the screen time of the most important character in Batman than the least important one? most important character refers to max(screentime); least important character refers to min(screentime); movie Batman refers to title = 'Batman'; percentage = divide(subtract(max(screentime) , min(screentime)) , min(screentime)) * 100% */
SELECT 
SELECT (MAX(CAST(SUBSTR(T2.screentime, 3, 2) AS REAL)) - MIN(CAST(SUBSTR(T2.screentime, 3, 2) AS REAL))) * 100 / MIN(CAST(SUBSTR(T2.screentime, 3, 2) AS REAL)) FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID WHERE T1.Title = 'Batman'
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: Which movie had the biggest budget? Give the name of the movie. biggest budget refers to max(Budget); name of the movie refers to Title */
SELECT 
SELECT Title FROM movie ORDER BY Budget DESC LIMIT 1
/* Given the following database schema: */
CREATE TABLE actor
(
    ActorID           INTEGER
        constraint actor_pk
            primary key,
    Name              TEXT,
    "Date of Birth"   DATE,
    "Birth City"      TEXT,
    "Birth Country"   TEXT,
    "Height (Inches)" INTEGER,
    Biography         TEXT,
    Gender            TEXT,
    Ethnicity         TEXT,
    NetWorth          TEXT
)

CREATE TABLE movie
(
    MovieID        INTEGER
        constraint movie_pk
            primary key,
    Title          TEXT,
    "MPAA Rating"  TEXT,
    Budget         INTEGER,
    Gross          INTEGER,
    "Release Date" TEXT,
    Genre          TEXT,
    Runtime        INTEGER,
    Rating         REAL,
    "Rating Count" INTEGER,
    Summary        TEXT
)

CREATE TABLE characters
(
    MovieID          INTEGER,
    ActorID          INTEGER,
    "Character Name" TEXT,
    creditOrder      INTEGER,
    pay              TEXT,
    screentime       TEXT,
    primary key (MovieID, ActorID),
    foreign key (ActorID) references actor(ActorID),
    foreign key (MovieID) references movie(MovieID)
)

/* Answer the following: What is the MPAA rating for the movie with the character named "Peter Quill" in it? MPAA rating = 'G' means General audiences; MPAA rating = 'PG' means Parental guidance suggested; MPAA rating = 'R'means Restricted; MPAA rating = 'X' means No one under 17 admitted */
SELECT 
SELECT T1.`MPAA Rating` FROM movie AS T1 INNER JOIN characters AS T2 ON T1.MovieID = T2.MovieID WHERE T2.`Character Name` = 'Peter Quill'