diff --git a/sql-queries-11/single-row-from-join/create-table.sql b/sql-queries-11/single-row-from-join/create-table.sql new file mode 100644 index 00000000..540ed334 --- /dev/null +++ b/sql-queries-11/single-row-from-join/create-table.sql @@ -0,0 +1,35 @@ +CREATE TABLE IF NOT EXISTS countries +( + country_id integer NOT NULL, + country_name text, + CONSTRAINT countries_pkey PRIMARY KEY (country_id) +); + +CREATE TABLE IF NOT EXISTS cities +( + city_id integer NOT NULL, + country_id integer, + city_name text, + population bigint, + CONSTRAINT cities_pkey PRIMARY KEY (city_id), + CONSTRAINT country_id FOREIGN KEY (country_id) + REFERENCES countries (country_id) +); + +INSERT INTO countries (country_id, country_name) + VALUES + (1, 'USA'), + (2, 'UK'), + (3, 'France'), + (4, 'Thailand'); + +INSERT INTO cities (city_id, country_id, city_name, population) + VALUES + ( 1, 1, 'Washington',689545), + ( 2,1,'New York', 8804190), + ( 3, 2, 'London', 12208100 ), + ( 4 , 2, 'Manchester', 2732854), + ( 5, 3, 'Paris', 2048472), + ( 6, 3, 'Nice', 353701), + ( 7, 4, 'Bangkok', 5588222), + ( 8, 4, 'Phuket', 77778); diff --git a/sql-queries-11/single-row-from-join/single-row-from-join.sql b/sql-queries-11/single-row-from-join/single-row-from-join.sql new file mode 100644 index 00000000..a4c254fb --- /dev/null +++ b/sql-queries-11/single-row-from-join/single-row-from-join.sql @@ -0,0 +1,14 @@ +SELECT countries.country_id, country_name, city_id, city_name, population +FROM countries +JOIN cities ON countries.country_id = cities.country_id; + +SELECT DISTINCT ON(countries.country_id) + countries.country_id, country_name, city_id, city_name, population +FROM countries +JOIN cities ON countries.country_id = cities.country_id; + +SELECT DISTINCT ON(countries.country_id) + countries.country_id, country_name, city_id, city_name, population +FROM countries +JOIN cities ON countries.country_id = cities.country_id +ORDER BY countries.country_id, population DESC;