From 3f3a4963e398e7f0b3ca4afad7b31ca81a050ee4 Mon Sep 17 00:00:00 2001 From: Callum Gibbons Date: Sun, 1 Jun 2025 17:07:49 +0100 Subject: [PATCH 1/5] [SQL-378] - How to join only one row in joined table in SQL --- .../single-row-from-join/create-table.sql | 35 +++++++++++++++++++ .../single-row-from-join.sql | 0 2 files changed, 35 insertions(+) create mode 100644 sql-queries-11/single-row-from-join/create-table.sql create mode 100644 sql-queries-11/single-row-from-join/single-row-from-join.sql 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..335fb577 --- /dev/null +++ b/sql-queries-11/single-row-from-join/create-table.sql @@ -0,0 +1,35 @@ +CREATE TABLE IF NOT EXISTS public +( + 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); \ No newline at end of file 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..e69de29b From 0cec416c17a661d51bcac2147be61c903dac0807 Mon Sep 17 00:00:00 2001 From: Callum Gibbons Date: Tue, 3 Jun 2025 16:06:20 +0100 Subject: [PATCH 2/5] Added unsaved changes --- sql-queries-11/single-row-from-join/create-table.sql | 2 +- .../single-row-from-join/single-row-from-join.sql | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sql-queries-11/single-row-from-join/create-table.sql b/sql-queries-11/single-row-from-join/create-table.sql index 335fb577..649ae4fc 100644 --- a/sql-queries-11/single-row-from-join/create-table.sql +++ b/sql-queries-11/single-row-from-join/create-table.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS public +CREATE TABLE IF NOT EXISTS countries ( country_id integer NOT NULL, country_name text , 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 index e69de29b..5ef71af6 100644 --- 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 @@ -0,0 +1,11 @@ +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; \ No newline at end of file From f9c676184896af1a971450ada5926b014e993d6b Mon Sep 17 00:00:00 2001 From: Jimmy Azar <76515490+ykxvqze@users.noreply.github.com> Date: Tue, 10 Jun 2025 20:39:53 +0300 Subject: [PATCH 3/5] Update create-table.sql --- sql-queries-11/single-row-from-join/create-table.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sql-queries-11/single-row-from-join/create-table.sql b/sql-queries-11/single-row-from-join/create-table.sql index 649ae4fc..8fd39e75 100644 --- a/sql-queries-11/single-row-from-join/create-table.sql +++ b/sql-queries-11/single-row-from-join/create-table.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS countries ( country_id integer NOT NULL, - country_name text , + country_name text, CONSTRAINT countries_pkey PRIMARY KEY (country_id) ); @@ -25,11 +25,11 @@ INSERT INTO countries (country_id, country_name) INSERT INTO cities (city_id, country_id, city_name, population) VALUES - ( 1, 1, 'Washington',689545), + ( 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); \ No newline at end of file + ( 8, 4, 'Phuket', 77778); From 3de6ae53fea3038991989e725d5be8204118d546 Mon Sep 17 00:00:00 2001 From: Jimmy Azar <76515490+ykxvqze@users.noreply.github.com> Date: Tue, 10 Jun 2025 20:40:40 +0300 Subject: [PATCH 4/5] Update create-table.sql --- sql-queries-11/single-row-from-join/create-table.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-queries-11/single-row-from-join/create-table.sql b/sql-queries-11/single-row-from-join/create-table.sql index 8fd39e75..540ed334 100644 --- a/sql-queries-11/single-row-from-join/create-table.sql +++ b/sql-queries-11/single-row-from-join/create-table.sql @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS cities 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 From dded59d6c2ba430baf9427011da737411cda8958 Mon Sep 17 00:00:00 2001 From: Jimmy Azar <76515490+ykxvqze@users.noreply.github.com> Date: Tue, 10 Jun 2025 20:42:33 +0300 Subject: [PATCH 5/5] Update single-row-from-join.sql --- .../single-row-from-join.sql | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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 index 5ef71af6..a4c254fb 100644 --- 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 @@ -1,11 +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 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; + 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; \ No newline at end of file + 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;