db_id
stringclasses
146 values
question
stringlengths
3
224
sql
stringlengths
18
577
database_schema
stringclasses
146 values
products_gen_characteristics
How many colors are never used by any product?
SELECT count(*) FROM Ref_colors WHERE color_code NOT IN ( SELECT color_code FROM products )
CREATE TABLE `Ref_Characteristic_Types` ( `characteristic_type_code` VARCHAR(15) PRIMARY KEY, `characteristic_type_description` VARCHAR(80) ) 3 rows from Ref_Characteristic_Types table: characteristic_type_code characteristic_type_description Grade Grade Purity Purity CREATE TABLE `Ref_Colors` ( `color_code` VARCHAR(15) PRIMARY KEY, `color_description` VARCHAR(80) ) 3 rows from Ref_Colors table: color_code color_description 9 red 5 green 1 yellow CREATE TABLE `Ref_Product_Categories` ( `product_category_code` VARCHAR(15) PRIMARY KEY, `product_category_description` VARCHAR(80), `unit_of_measure` VARCHAR(20) ) 3 rows from Ref_Product_Categories table: product_category_code product_category_description unit_of_measure Herbs Herbs Handful Seeds Seeds Weight - pound,kilo. Spices Spices Weight - pound,kilo. CREATE TABLE `Characteristics` ( `characteristic_id` INTEGER PRIMARY KEY, `characteristic_type_code` VARCHAR(15) NOT NULL, `characteristic_data_type` VARCHAR(10), `characteristic_name` VARCHAR(80), `other_characteristic_details` VARCHAR(255), FOREIGN KEY (`characteristic_type_code` ) REFERENCES `Ref_Characteristic_Types`(`characteristic_type_code` ) ) 3 rows from Characteristics table: characteristic_id characteristic_type_code characteristic_data_type characteristic_name other_characteristic_details 1 Grade numquam slow None 2 Grade doloribus fast None 3 Purity rem warm None CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `color_code` VARCHAR(15) NOT NULL, `product_category_code` VARCHAR(15) NOT NULL, `product_name` VARCHAR(80), `typical_buying_price` VARCHAR(20), `typical_selling_price` VARCHAR(20), `product_description` VARCHAR(255), `other_product_details` VARCHAR(255), FOREIGN KEY (`product_category_code` ) REFERENCES `Ref_Product_Categories`(`product_category_code` ),FOREIGN KEY (`color_code` ) REFERENCES `Ref_Colors`(`color_code` ) ) 3 rows from Products table: product_id color_code product_category_code product_name typical_buying_price typical_selling_price product_description other_product_details 1 4 Spices cumin 2878.3 et None 2 2 Spices peper 352447.2874677 1892070.2803543 rerum None 3 9 Herbs basil 503.8431967 0.1859512 officia None CREATE TABLE `Product_Characteristics` ( `product_id` INTEGER NOT NULL, `characteristic_id` INTEGER NOT NULL, `product_characteristic_value` VARCHAR(50), FOREIGN KEY (`characteristic_id` ) REFERENCES `Characteristics`(`characteristic_id` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) ) 3 rows from Product_Characteristics table: product_id characteristic_id product_characteristic_value 13 13 low 11 2 low 5 15 low
products_gen_characteristics
Count the number of colors that are not used in any products.
SELECT count(*) FROM Ref_colors WHERE color_code NOT IN ( SELECT color_code FROM products )
CREATE TABLE `Ref_Characteristic_Types` ( `characteristic_type_code` VARCHAR(15) PRIMARY KEY, `characteristic_type_description` VARCHAR(80) ) 3 rows from Ref_Characteristic_Types table: characteristic_type_code characteristic_type_description Grade Grade Purity Purity CREATE TABLE `Ref_Colors` ( `color_code` VARCHAR(15) PRIMARY KEY, `color_description` VARCHAR(80) ) 3 rows from Ref_Colors table: color_code color_description 9 red 5 green 1 yellow CREATE TABLE `Ref_Product_Categories` ( `product_category_code` VARCHAR(15) PRIMARY KEY, `product_category_description` VARCHAR(80), `unit_of_measure` VARCHAR(20) ) 3 rows from Ref_Product_Categories table: product_category_code product_category_description unit_of_measure Herbs Herbs Handful Seeds Seeds Weight - pound,kilo. Spices Spices Weight - pound,kilo. CREATE TABLE `Characteristics` ( `characteristic_id` INTEGER PRIMARY KEY, `characteristic_type_code` VARCHAR(15) NOT NULL, `characteristic_data_type` VARCHAR(10), `characteristic_name` VARCHAR(80), `other_characteristic_details` VARCHAR(255), FOREIGN KEY (`characteristic_type_code` ) REFERENCES `Ref_Characteristic_Types`(`characteristic_type_code` ) ) 3 rows from Characteristics table: characteristic_id characteristic_type_code characteristic_data_type characteristic_name other_characteristic_details 1 Grade numquam slow None 2 Grade doloribus fast None 3 Purity rem warm None CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `color_code` VARCHAR(15) NOT NULL, `product_category_code` VARCHAR(15) NOT NULL, `product_name` VARCHAR(80), `typical_buying_price` VARCHAR(20), `typical_selling_price` VARCHAR(20), `product_description` VARCHAR(255), `other_product_details` VARCHAR(255), FOREIGN KEY (`product_category_code` ) REFERENCES `Ref_Product_Categories`(`product_category_code` ),FOREIGN KEY (`color_code` ) REFERENCES `Ref_Colors`(`color_code` ) ) 3 rows from Products table: product_id color_code product_category_code product_name typical_buying_price typical_selling_price product_description other_product_details 1 4 Spices cumin 2878.3 et None 2 2 Spices peper 352447.2874677 1892070.2803543 rerum None 3 9 Herbs basil 503.8431967 0.1859512 officia None CREATE TABLE `Product_Characteristics` ( `product_id` INTEGER NOT NULL, `characteristic_id` INTEGER NOT NULL, `product_characteristic_value` VARCHAR(50), FOREIGN KEY (`characteristic_id` ) REFERENCES `Characteristics`(`characteristic_id` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) ) 3 rows from Product_Characteristics table: product_id characteristic_id product_characteristic_value 13 13 low 11 2 low 5 15 low
swimming
How many events are there?
SELECT count(*) FROM event
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
List all the event names by year from the most recent to the oldest.
SELECT name FROM event ORDER BY YEAR DESC
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
What is the name of the event that happened in the most recent year?
SELECT name FROM event ORDER BY YEAR DESC LIMIT 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
How many stadiums are there?
SELECT count(*) FROM stadium
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the name of the stadium that has the maximum capacity.
SELECT name FROM stadium ORDER BY capacity DESC LIMIT 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of stadiums whose capacity is smaller than the average capacity.
SELECT name FROM stadium WHERE capacity < (SELECT avg(capacity) FROM stadium)
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the country that has the most stadiums.
SELECT country FROM stadium GROUP BY country ORDER BY count(*) DESC LIMIT 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Which country has at most 3 stadiums listed?
SELECT country FROM stadium GROUP BY country HAVING count(*) <= 3
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Which country has both stadiums with capacity greater than 60000 and stadiums with capacity less than 50000?
SELECT country FROM stadium WHERE capacity > 60000 INTERSECT SELECT country FROM stadium WHERE capacity < 50000
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
How many cities have a stadium that was opened before the year of 2006?
SELECT count(DISTINCT city) FROM stadium WHERE opening_year < 2006
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
How many stadiums does each country have?
SELECT country , count(*) FROM stadium GROUP BY country
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Which countries do not have a stadium that was opened after 2006?
SELECT country FROM stadium EXCEPT SELECT country FROM stadium WHERE opening_year > 2006
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
How many stadiums are not in country "Russia"?
SELECT count(*) FROM stadium WHERE country != 'Russia'
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of all swimmers, sorted by their 100 meter scores in ascending order.
SELECT name FROM swimmer ORDER BY meter_100
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
How many different countries are all the swimmers from?
SELECT count(DISTINCT nationality) FROM swimmer
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
List countries that have more than one swimmer.
SELECT nationality , count(*) FROM swimmer GROUP BY nationality HAVING count(*) > 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find all 200 meter and 300 meter results of swimmers with nationality "Australia".
SELECT meter_200 , meter_300 FROM swimmer WHERE nationality = 'Australia'
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of swimmers who has a result of "win".
SELECT t1.name FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id WHERE RESULT = 'Win'
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
What is the name of the stadium which held the most events?
SELECT t1.name FROM stadium AS t1 JOIN event AS t2 ON t1.id = t2.stadium_id GROUP BY t2.stadium_id ORDER BY count(*) DESC LIMIT 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the name and capacity of the stadium where the event named "World Junior" happened.
SELECT t1.name , t1.capacity FROM stadium AS t1 JOIN event AS t2 ON t1.id = t2.stadium_id WHERE t2.name = 'World Junior'
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of stadiums which have never had any event.
SELECT name FROM stadium WHERE id NOT IN (SELECT stadium_id FROM event)
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the name of the swimmer who has the most records.
SELECT t1.name FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id GROUP BY t2.swimmer_id ORDER BY count(*) DESC LIMIT 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the name of the swimmer who has at least 2 records.
SELECT t1.name FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id GROUP BY t2.swimmer_id HAVING count(*) >= 2
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the name and nationality of the swimmer who has won (i.e., has a result of "win") more than 1 time.
SELECT t1.name , t1.nationality FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id WHERE RESULT = 'Win' GROUP BY t2.swimmer_id HAVING count(*) > 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of the swimmers who have no record.
SELECT name FROM swimmer WHERE id NOT IN (SELECT swimmer_id FROM record)
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of the swimmers who have both "win" and "loss" results in the record.
SELECT t1.name FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id WHERE RESULT = 'Win' INTERSECT SELECT t1.name FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id WHERE RESULT = 'Loss'
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of stadiums that some Australian swimmers have been to.
SELECT t4.name FROM swimmer AS t1 JOIN record AS t2 ON t1.id = t2.swimmer_id JOIN event AS t3 ON t2.event_id = t3.id JOIN stadium AS t4 ON t4.id = t3.stadium_id WHERE t1.nationality = 'Australia'
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find the names of stadiums that the most swimmers have been to.
SELECT t3.name FROM record AS t1 JOIN event AS t2 ON t1.event_id = t2.id JOIN stadium AS t3 ON t3.id = t2.stadium_id GROUP BY t2.stadium_id ORDER BY count(*) DESC LIMIT 1
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
Find all details for each swimmer.
SELECT * FROM swimmer
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
swimming
What is the average capacity of the stadiums that were opened in year 2005?
SELECT avg(capacity) FROM stadium WHERE opening_year = 2005
CREATE TABLE "swimmer" ( "ID" int, "name" text, "Nationality" text, "meter_100" real, "meter_200" text, "meter_300" text, "meter_400" text, "meter_500" text, "meter_600" text, "meter_700" text, "Time" text, PRIMARY KEY ("ID") ) 3 rows from swimmer table: ID name Nationality meter_100 meter_200 meter_300 meter_400 meter_500 meter_600 meter_700 Time 7 Przemysław Stańczyk Poland 57.31 1:57.10 2:56.02 3:55.36 4:54.21 5:52.59 6:50.91 7:47.91 4 Craig Stevens Australia 57.35 1:56.34 2:55.90 3:55.72 4:55.08 5:54.45 6:52.69 7:48.67 5 Federico Colbertaldo Italy 57.66 1:56.77 2:56.04 3:55.37 4:54.48 5:53.53 6:52.58 7:49.98 CREATE TABLE "stadium" ( "ID" int, "name" text, "Capacity" int, "City" text, "Country" text, "Opening_year" int, PRIMARY KEY ("ID") ) 3 rows from stadium table: ID name Capacity City Country Opening_year 1 Nou Mestalla 75000 Valencia Spain 2004 2 Gazprom Arena 69501 Saint Petersburg Russia 2005 3 Baku Olympic Stadium 68000 Baku Azerbaijan 2005 CREATE TABLE "event" ( "ID" int, "Name" text, "Stadium_ID" int, "Year" text, PRIMARY KEY ("ID"), FOREIGN KEY (`Stadium_ID`) REFERENCES `stadium`(`ID`) ) 3 rows from event table: ID Name Stadium_ID Year 1 FINA 1 2016 2 Pacific 10 2011 3 World Master 2 2012 CREATE TABLE "record" ( "ID" int, "Result" text, "Swimmer_ID" int, "Event_ID" int, PRIMARY KEY ("Swimmer_ID","Event_ID"), FOREIGN KEY (`Event_ID`) REFERENCES `event`(`ID`), FOREIGN KEY (`Swimmer_ID`) REFERENCES `swimmer`(`ID`) ) 3 rows from record table: ID Result Swimmer_ID Event_ID 1 NC 1 1 2 Win 1 2 3 Loss 3 3
railway
How many railways are there?
SELECT count(*) FROM railway
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
List the builders of railways in ascending alphabetical order.
SELECT Builder FROM railway ORDER BY Builder ASC
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
List the wheels and locations of the railways.
SELECT Wheels , LOCATION FROM railway
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
What is the maximum level of managers in countries that are not "Australia"?
SELECT max(LEVEL) FROM manager WHERE Country != "Australia "
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
What is the average age for all managers?
SELECT avg(Age) FROM manager
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
What are the names of managers in ascending order of level?
SELECT Name FROM manager ORDER BY LEVEL ASC
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
What are the names and arrival times of trains?
SELECT Name , Arrival FROM train
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
What is the name of the oldest manager?
SELECT Name FROM manager ORDER BY Age DESC LIMIT 1
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the names of trains and locations of railways they are in.
SELECT T2.Name , T1.Location FROM railway AS T1 JOIN train AS T2 ON T1.Railway_ID = T2.Railway_ID
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the builder of railways associated with the trains named "Andaman Exp".
SELECT T1.Builder FROM railway AS T1 JOIN train AS T2 ON T1.Railway_ID = T2.Railway_ID WHERE T2.Name = "Andaman Exp"
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show id and location of railways that are associated with more than one train.
SELECT T2.Railway_ID , T1.Location FROM railway AS T1 JOIN train AS T2 ON T1.Railway_ID = T2.Railway_ID GROUP BY T2.Railway_ID HAVING COUNT(*) > 1
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the id and builder of the railway that are associated with the most trains.
SELECT T2.Railway_ID , T1.Builder FROM railway AS T1 JOIN train AS T2 ON T1.Railway_ID = T2.Railway_ID GROUP BY T2.Railway_ID ORDER BY COUNT(*) DESC LIMIT 1
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show different builders of railways, along with the corresponding number of railways using each builder.
SELECT Builder , COUNT(*) FROM railway GROUP BY Builder
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the most common builder of railways.
SELECT Builder FROM railway GROUP BY Builder ORDER BY COUNT(*) DESC LIMIT 1
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show different locations of railways along with the corresponding number of railways at each location.
SELECT LOCATION , COUNT(*) FROM railway GROUP BY LOCATION
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the locations that have more than one railways.
SELECT LOCATION FROM railway GROUP BY LOCATION HAVING COUNT(*) > 1
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
List the object number of railways that do not have any trains.
SELECT ObjectNumber FROM railway WHERE Railway_ID NOT IN (SELECT Railway_ID FROM train)
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the countries that have both managers of age above 50 and managers of age below 46.
SELECT Country FROM manager WHERE Age > 50 INTERSECT SELECT Country FROM manager WHERE Age < 46
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the distinct countries of managers.
SELECT DISTINCT Country FROM manager
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the working years of managers in descending order of their level.
SELECT Working_year_starts FROM manager ORDER BY LEVEL DESC
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
railway
Show the countries that have managers of age above 50 or below 46.
SELECT Country FROM manager WHERE Age > 50 OR Age < 46
CREATE TABLE "railway" ( "Railway_ID" int, "Railway" text, "Builder" text, "Built" text, "Wheels" text, "Location" text, "ObjectNumber" text, PRIMARY KEY ("Railway_ID") ) 3 rows from railway table: Railway_ID Railway Builder Built Wheels Location ObjectNumber 1 SECR SECR Ashford 1901 4-4-0 York 1975-7006 2 MR MR Derby 1902 Midland Railway 1000 was rebuilt in 1914. 4-4-0 Bo'ness 1975-7018 3 GNRD GNR Doncaster 1902 4-4-2 Barrow Hill 1975-7005 CREATE TABLE "train" ( "Train_ID" int, "Train_Num" text, "Name" text, "From" text, "Arrival" text, "Railway_ID" int, PRIMARY KEY ("Train_ID"), FOREIGN KEY ("Railway_ID") REFERENCES `railway`("Railway_ID") ) 3 rows from train table: Train_ID Train_Num Name From Arrival Railway_ID 1 51195 Wardha-Ballarshah Pass Wardha 08:54 1 2 12139 Sewagram Exp Mumbai CST 09:08 1 3 12140 Ballarshah-Mumbai Pass Ballarshah 09:48 2 CREATE TABLE "manager" ( "Manager_ID" int, "Name" text, "Country" text, "Working_year_starts" text, "Age" int, "Level" int, PRIMARY KEY ("Manager_ID") ) 3 rows from manager table: Manager_ID Name Country Working_year_starts Age Level 1 Ben Curtis United States 2003 45 5 2 Todd Hamilton United States 2004 55 5 3 Tiger Woods United States 2006 46 5 CREATE TABLE "railway_manage" ( "Railway_ID" int, "Manager_ID" int, "From_Year" text, PRIMARY KEY ("Railway_ID","Manager_ID"), FOREIGN KEY ("Manager_ID") REFERENCES "manager"("Manager_ID"), FOREIGN KEY ("Railway_ID") REFERENCES "railway"("Railway_ID") ) 3 rows from railway_manage table: Railway_ID Manager_ID From_Year 8 1 2010 9 2 2011 7 3 2012
customers_and_products_contacts
How many addresses are there in country USA?
SELECT count(*) FROM addresses WHERE country = 'USA'
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show all distinct cities in the address record.
SELECT DISTINCT city FROM addresses
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show each state and the number of addresses in each state.
SELECT state_province_county , count(*) FROM addresses GROUP BY state_province_county
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show names and phones of customers who do not have address information.
SELECT customer_name , customer_phone FROM customers WHERE customer_id NOT IN (SELECT customer_id FROM customer_address_history)
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the name of the customer who has the most orders.
SELECT T1.customer_name FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the product type codes which have at least two products.
SELECT product_type_code FROM products GROUP BY product_type_code HAVING count(*) >= 2
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the names of customers who have both an order in completed status and an order in part status.
SELECT T1.customer_name FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status_code = 'Completed' INTERSECT SELECT T1.customer_name FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id = T2.customer_id WHERE T2.order_status_code = 'Part'
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the name, phone, and payment method code for all customers in descending order of customer number.
SELECT customer_name , customer_phone , payment_method_code FROM customers ORDER BY customer_number DESC
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the product name and total order quantity for each product.
SELECT T1.product_name , sum(T2.order_quantity) FROM products AS T1 JOIN order_items AS T2 ON T1.product_id = T2.product_id GROUP BY T1.product_id
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the minimum, maximum, average price for all products.
SELECT min(product_price) , max(product_price) , avg(product_price) FROM products
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
How many products have a price higher than the average?
SELECT count(*) FROM products WHERE product_price > (SELECT avg(product_price) FROM products)
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the customer name, customer address city, date from, and date to for each customer address history.
SELECT T2.customer_name , T3.city , T1.date_from , T1.date_to FROM customer_address_history AS T1 JOIN customers AS T2 ON T1.customer_id = T2.customer_id JOIN addresses AS T3 ON T1.address_id = T3.address_id
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the names of customers who use Credit Card payment method and have more than 2 orders.
SELECT T1.customer_name FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id = T2.customer_id WHERE T1.payment_method_code = 'Credit Card' GROUP BY T1.customer_id HAVING count(*) > 2
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
What are the name and phone of the customer with the most ordered product quantity?
SELECT T1.customer_name , T1.customer_phone FROM customers AS T1 JOIN customer_orders AS T2 ON T1.customer_id = T2.customer_id JOIN order_items AS T3 ON T3.order_id = T2.order_id GROUP BY T1.customer_id ORDER BY sum(T3.order_quantity) DESC LIMIT 1
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
customers_and_products_contacts
Show the product type and name for the products with price higher than 1000 or lower than 500.
SELECT product_type_code , product_name FROM products WHERE product_price > 1000 OR product_price < 500
CREATE TABLE `Addresses` ( `address_id` INTEGER PRIMARY KEY, `line_1_number_building` VARCHAR(80), `city` VARCHAR(50), `zip_postcode` VARCHAR(20), `state_province_county` VARCHAR(50), `country` VARCHAR(50) ) 3 rows from Addresses table: address_id line_1_number_building city zip_postcode state_province_county country 1 4315 Kerluke Canyon Apt. 800 Hertafurt 740 Nevada USA 2 319 Kozey Highway Suite 973 Edgardoberg 282 Colorado USA 3 592 Frederique Ridge Gilbertmouth 167 Virginia USA CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `product_type_code` VARCHAR(15), `product_name` VARCHAR(80), `product_price` DOUBLE NULL ) 3 rows from Products table: product_id product_type_code product_name product_price 1 Hardware Apple 5.475398e+07 2 Clothes jcrew 3.059093e+07 3 Hardware Apple 1.026885e+04 CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `payment_method_code` VARCHAR(15), `customer_number` VARCHAR(20), `customer_name` VARCHAR(80), `customer_address` VARCHAR(255), `customer_phone` VARCHAR(80), `customer_email` VARCHAR(80) ) 3 rows from Customers table: customer_id payment_method_code customer_number customer_name customer_address customer_phone customer_email 1 Credit Card 456 Kayley 636 Chanelle Isle Apt. 846 +87(9)5279161988 antonette73@example.com 2 Credit Card 553 Sterling 12174 Boyer Crossroad 896.685.8228x2786 stroman.chadd@example.net 3 Credit Card 951 Buford 650 Spencer Way Apt. 584 (192)144-4687 pattie.mayer@example.net CREATE TABLE `Contacts` ( `contact_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `gender` VARCHAR(1), `first_name` VARCHAR(80), `last_name` VARCHAR(50), `contact_phone` VARCHAR(80) ) 3 rows from Contacts table: contact_id customer_id gender first_name last_name contact_phone 1 4 male Cierra Collins +73(2)0854391820 2 6 male Jennifer Doyle 482-949-1364x17500 3 8 female Carli Blick (608)868-5069x554 CREATE TABLE `Customer_Address_History` ( `customer_id` INTEGER NOT NULL, `address_id` INTEGER NOT NULL, `date_from` DATETIME NOT NULL, `date_to` DATETIME, FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ), FOREIGN KEY (`address_id` ) REFERENCES `Addresses`(`address_id` ) ) 3 rows from Customer_Address_History table: customer_id address_id date_from date_to 12 7 2015-07-23 14:37:18 2018-03-07 12:04:20 12 2 2016-11-06 14:33:12 2018-03-14 21:36:28 3 9 2011-11-19 12:17:36 2018-03-22 10:20:16 CREATE TABLE `Customer_Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `order_date` DATETIME NOT NULL, `order_status_code` VARCHAR(15), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ) 3 rows from Customer_Orders table: order_id customer_id order_date order_status_code 1 2 2009-07-19 13:40:49 Completed 2 2 1976-05-28 15:02:44 Part 3 6 1979-03-29 02:47:13 Completed CREATE TABLE `Order_Items` ( `order_item_id` INTEGER NOT NULL , `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `order_quantity` VARCHAR(80), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Customer_Orders`(`order_id` ) ) 3 rows from Order_Items table: order_item_id order_id product_id order_quantity 1 9 15 3 2 8 12 7 3 11 9 9
dorm_1
Find the name of dorms only for female (F gender).
SELECT dorm_name FROM dorm WHERE gender = 'F'
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What are the names of the all-female dorms?
SELECT dorm_name FROM dorm WHERE gender = 'F'
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the name of dorms that can accommodate more than 300 students.
SELECT dorm_name FROM dorm WHERE student_capacity > 300
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What are the names of all the dorms that can accomdate more than 300 students?
SELECT dorm_name FROM dorm WHERE student_capacity > 300
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many female students (sex is F) whose age is below 25?
SELECT count(*) FROM student WHERE sex = 'F' AND age < 25
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many girl students who are younger than 25?
SELECT count(*) FROM student WHERE sex = 'F' AND age < 25
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the first name of students who is older than 20.
SELECT fname FROM student WHERE age > 20
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What are the first names of all students who are older than 20?
SELECT fname FROM student WHERE age > 20
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the first name of students living in city PHL whose age is between 20 and 25.
SELECT fname FROM student WHERE city_code = 'PHL' AND age BETWEEN 20 AND 25
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What is the first name of the students who are in age 20 to 25 and living in PHL city?
SELECT fname FROM student WHERE city_code = 'PHL' AND age BETWEEN 20 AND 25
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many dorms are there?
SELECT count(*) FROM dorm
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many dorms are in the database?
SELECT count(*) FROM dorm
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the number of distinct amenities.
SELECT count(*) FROM dorm_amenity
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many diffrent dorm amenities are there?
SELECT count(*) FROM dorm_amenity
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the total capacity of all dorms.
SELECT sum(student_capacity) FROM dorm
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What is the total student capacity of all dorms?
SELECT sum(student_capacity) FROM dorm
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many students are there?
SELECT count(*) FROM student
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many students exist?
SELECT count(*) FROM student
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the average age of all students living in the each city.
SELECT avg(age) , city_code FROM student GROUP BY city_code
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What is the average age for each city and what are those cities?
SELECT avg(age) , city_code FROM student GROUP BY city_code
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the average and total capacity of dorms for the students with gender X.
SELECT avg(student_capacity) , sum(student_capacity) FROM dorm WHERE gender = 'X'
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What is the average and total capacity for all dorms who are of gender X?
SELECT avg(student_capacity) , sum(student_capacity) FROM dorm WHERE gender = 'X'
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the number of dorms that have some amenity.
SELECT count(DISTINCT dormid) FROM has_amenity
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many dorms have amenities?
SELECT count(DISTINCT dormid) FROM has_amenity
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the name of dorms that do not have any amenity
SELECT dorm_name FROM dorm WHERE dormid NOT IN (SELECT dormid FROM has_amenity)
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What are the names of all the dorms that don't have any amenities?
SELECT dorm_name FROM dorm WHERE dormid NOT IN (SELECT dormid FROM has_amenity)
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the number of distinct gender for dorms.
SELECT count(DISTINCT gender) FROM dorm
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
How many different genders are there in the dorms?
SELECT count(DISTINCT gender) FROM dorm
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the capacity and gender type of the dorm whose name has substring ‘Donor’.
SELECT student_capacity , gender FROM dorm WHERE dorm_name LIKE '%Donor%'
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What is the student capacity and type of gender for the dorm whose name as the phrase Donor in it?
SELECT student_capacity , gender FROM dorm WHERE dorm_name LIKE '%Donor%'
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
Find the name and gender type of the dorms whose capacity is greater than 300 or less than 100.
SELECT dorm_name , gender FROM dorm WHERE student_capacity > 300 OR student_capacity < 100
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124
dorm_1
What are the names and types of the dorms that have a capacity greater than 300 or less than 100?
SELECT dorm_name , gender FROM dorm WHERE student_capacity > 300 OR student_capacity < 100
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) ) 3 rows from Student table: StuID LName Fname Age Sex Major Advisor city_code 1001 Smith Linda 18 F 600 1121 BAL 1002 Kim Tracy 19 F 600 7712 HKG 1003 Jones Shiela 21 F 600 7792 WAS CREATE TABLE Dorm ( dormid INTEGER, dorm_name VARCHAR(20), student_capacity INTEGER, gender VARCHAR(1) ) 3 rows from Dorm table: dormid dorm_name student_capacity gender 100 Smith Hall 85 X 110 Bud Jones Hall 116 M 140 Fawlty Towers 355 X CREATE TABLE Dorm_amenity ( amenid INTEGER, amenity_name VARCHAR(25) ) 3 rows from Dorm_amenity table: amenid amenity_name 900 TV Lounge 901 Study Room 902 Pub in Basement CREATE TABLE Has_amenity ( dormid INTEGER, amenid INTEGER, FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid), FOREIGN KEY (amenid) REFERENCES `Dorm_amenity`(amenid) ) 3 rows from Has_amenity table: dormid amenid 109 900 109 901 109 903 CREATE TABLE Lives_in ( stuid INTEGER, dormid INTEGER, room_number INTEGER, FOREIGN KEY (stuid) REFERENCES `Student`(StuID), FOREIGN KEY (dormid) REFERENCES `Dorm`(dormid) ) 3 rows from Lives_in table: stuid dormid room_number 1001 109 105 1002 100 112 1003 100 124