Query about companies and regions
I'm doing some exercises about SQL right now.
I have a database with this schema:
CREATE TABLE regions(
region_id INTEGER PRIMARY KEY,
region_name TEXT NOT NULL,
number_of_customers INTEGER NOT NULL);
CREATE TABLE region_borders (
region_id1 INTEGER REFERENCES regions,
region_id2 INTEGER REFERENCES regions,
PRIMARY KEY (region_id1, region_id2));
CREATE TABLE companies (
company_id INTEGER PRIMARY KEY,
company_name TEXT NOT NULL,
headquarters_region INTEGER REFERENCES regions NOT NULL);
CREATE TABLE works_in(
company INTEGER REFERENCES companies,
region_id INTEGER REFERENCES regions,
PRIMARY KEY (company_id, region_id));
I want to translate to SQL this query:
"Retrieve the companies that works in three different regions that two by two share a border. Write for every company the company's name and the names of the 3 regions that share borders, in alphabetical order."
Well I have tried to work with joins and grouping, but I only achieved to get the names of companies that works in three different regions. I really have no idea about how implement this query in SQL or relational algebra.
I am using PostgreSQL.
Thank you for any advice and help!
sql database postgresql
|
show 2 more comments
I'm doing some exercises about SQL right now.
I have a database with this schema:
CREATE TABLE regions(
region_id INTEGER PRIMARY KEY,
region_name TEXT NOT NULL,
number_of_customers INTEGER NOT NULL);
CREATE TABLE region_borders (
region_id1 INTEGER REFERENCES regions,
region_id2 INTEGER REFERENCES regions,
PRIMARY KEY (region_id1, region_id2));
CREATE TABLE companies (
company_id INTEGER PRIMARY KEY,
company_name TEXT NOT NULL,
headquarters_region INTEGER REFERENCES regions NOT NULL);
CREATE TABLE works_in(
company INTEGER REFERENCES companies,
region_id INTEGER REFERENCES regions,
PRIMARY KEY (company_id, region_id));
I want to translate to SQL this query:
"Retrieve the companies that works in three different regions that two by two share a border. Write for every company the company's name and the names of the 3 regions that share borders, in alphabetical order."
Well I have tried to work with joins and grouping, but I only achieved to get the names of companies that works in three different regions. I really have no idea about how implement this query in SQL or relational algebra.
I am using PostgreSQL.
Thank you for any advice and help!
sql database postgresql
"Why should I provide an MCVE for what seems to me to be a very simple SQL query?" meta.stackoverflow.com/questions/333952/…
– Raymond Nijland
Nov 16 '18 at 12:48
3
Is that your homework?
– schlonzo
Nov 16 '18 at 12:50
Try combiningGROUP BY
withCOUNT DISTINCT
andHAVING
– schlonzo
Nov 16 '18 at 12:52
It is hard to suggest something solid without seeing table data but "Retrieve the companies that works in three different regions" That part you can easy get withSELECT companies.company_name, COUNT(*) FROM works_in INNER JOIN companies ON works_in.company = company.company_id GROUP BY works_in.company HAVING COUNT(DISTINCT region_id) >= 3
and use that result in a subquery and JOIN that with other tables to get the other information you need.. something like this is possible in SQL..SELECT * FROM ( SELECT ... ) AS alias INNER JOIN table ON alias.column = table.column ...
– Raymond Nijland
Nov 16 '18 at 12:53
Besides you can also write theSELECT ...
subquery in aINNER JOIN
clause like this.SELECT * FROM table INNER JOIN ( SELECT ...) AS alias ON table.column = alias.column
.. Good luck with your SQL exercises and SQL studies.
– Raymond Nijland
Nov 16 '18 at 12:58
|
show 2 more comments
I'm doing some exercises about SQL right now.
I have a database with this schema:
CREATE TABLE regions(
region_id INTEGER PRIMARY KEY,
region_name TEXT NOT NULL,
number_of_customers INTEGER NOT NULL);
CREATE TABLE region_borders (
region_id1 INTEGER REFERENCES regions,
region_id2 INTEGER REFERENCES regions,
PRIMARY KEY (region_id1, region_id2));
CREATE TABLE companies (
company_id INTEGER PRIMARY KEY,
company_name TEXT NOT NULL,
headquarters_region INTEGER REFERENCES regions NOT NULL);
CREATE TABLE works_in(
company INTEGER REFERENCES companies,
region_id INTEGER REFERENCES regions,
PRIMARY KEY (company_id, region_id));
I want to translate to SQL this query:
"Retrieve the companies that works in three different regions that two by two share a border. Write for every company the company's name and the names of the 3 regions that share borders, in alphabetical order."
Well I have tried to work with joins and grouping, but I only achieved to get the names of companies that works in three different regions. I really have no idea about how implement this query in SQL or relational algebra.
I am using PostgreSQL.
Thank you for any advice and help!
sql database postgresql
I'm doing some exercises about SQL right now.
I have a database with this schema:
CREATE TABLE regions(
region_id INTEGER PRIMARY KEY,
region_name TEXT NOT NULL,
number_of_customers INTEGER NOT NULL);
CREATE TABLE region_borders (
region_id1 INTEGER REFERENCES regions,
region_id2 INTEGER REFERENCES regions,
PRIMARY KEY (region_id1, region_id2));
CREATE TABLE companies (
company_id INTEGER PRIMARY KEY,
company_name TEXT NOT NULL,
headquarters_region INTEGER REFERENCES regions NOT NULL);
CREATE TABLE works_in(
company INTEGER REFERENCES companies,
region_id INTEGER REFERENCES regions,
PRIMARY KEY (company_id, region_id));
I want to translate to SQL this query:
"Retrieve the companies that works in three different regions that two by two share a border. Write for every company the company's name and the names of the 3 regions that share borders, in alphabetical order."
Well I have tried to work with joins and grouping, but I only achieved to get the names of companies that works in three different regions. I really have no idea about how implement this query in SQL or relational algebra.
I am using PostgreSQL.
Thank you for any advice and help!
sql database postgresql
sql database postgresql
asked Nov 16 '18 at 12:31
user10662775
"Why should I provide an MCVE for what seems to me to be a very simple SQL query?" meta.stackoverflow.com/questions/333952/…
– Raymond Nijland
Nov 16 '18 at 12:48
3
Is that your homework?
– schlonzo
Nov 16 '18 at 12:50
Try combiningGROUP BY
withCOUNT DISTINCT
andHAVING
– schlonzo
Nov 16 '18 at 12:52
It is hard to suggest something solid without seeing table data but "Retrieve the companies that works in three different regions" That part you can easy get withSELECT companies.company_name, COUNT(*) FROM works_in INNER JOIN companies ON works_in.company = company.company_id GROUP BY works_in.company HAVING COUNT(DISTINCT region_id) >= 3
and use that result in a subquery and JOIN that with other tables to get the other information you need.. something like this is possible in SQL..SELECT * FROM ( SELECT ... ) AS alias INNER JOIN table ON alias.column = table.column ...
– Raymond Nijland
Nov 16 '18 at 12:53
Besides you can also write theSELECT ...
subquery in aINNER JOIN
clause like this.SELECT * FROM table INNER JOIN ( SELECT ...) AS alias ON table.column = alias.column
.. Good luck with your SQL exercises and SQL studies.
– Raymond Nijland
Nov 16 '18 at 12:58
|
show 2 more comments
"Why should I provide an MCVE for what seems to me to be a very simple SQL query?" meta.stackoverflow.com/questions/333952/…
– Raymond Nijland
Nov 16 '18 at 12:48
3
Is that your homework?
– schlonzo
Nov 16 '18 at 12:50
Try combiningGROUP BY
withCOUNT DISTINCT
andHAVING
– schlonzo
Nov 16 '18 at 12:52
It is hard to suggest something solid without seeing table data but "Retrieve the companies that works in three different regions" That part you can easy get withSELECT companies.company_name, COUNT(*) FROM works_in INNER JOIN companies ON works_in.company = company.company_id GROUP BY works_in.company HAVING COUNT(DISTINCT region_id) >= 3
and use that result in a subquery and JOIN that with other tables to get the other information you need.. something like this is possible in SQL..SELECT * FROM ( SELECT ... ) AS alias INNER JOIN table ON alias.column = table.column ...
– Raymond Nijland
Nov 16 '18 at 12:53
Besides you can also write theSELECT ...
subquery in aINNER JOIN
clause like this.SELECT * FROM table INNER JOIN ( SELECT ...) AS alias ON table.column = alias.column
.. Good luck with your SQL exercises and SQL studies.
– Raymond Nijland
Nov 16 '18 at 12:58
"Why should I provide an MCVE for what seems to me to be a very simple SQL query?" meta.stackoverflow.com/questions/333952/…
– Raymond Nijland
Nov 16 '18 at 12:48
"Why should I provide an MCVE for what seems to me to be a very simple SQL query?" meta.stackoverflow.com/questions/333952/…
– Raymond Nijland
Nov 16 '18 at 12:48
3
3
Is that your homework?
– schlonzo
Nov 16 '18 at 12:50
Is that your homework?
– schlonzo
Nov 16 '18 at 12:50
Try combining
GROUP BY
with COUNT DISTINCT
and HAVING
– schlonzo
Nov 16 '18 at 12:52
Try combining
GROUP BY
with COUNT DISTINCT
and HAVING
– schlonzo
Nov 16 '18 at 12:52
It is hard to suggest something solid without seeing table data but "Retrieve the companies that works in three different regions" That part you can easy get with
SELECT companies.company_name, COUNT(*) FROM works_in INNER JOIN companies ON works_in.company = company.company_id GROUP BY works_in.company HAVING COUNT(DISTINCT region_id) >= 3
and use that result in a subquery and JOIN that with other tables to get the other information you need.. something like this is possible in SQL.. SELECT * FROM ( SELECT ... ) AS alias INNER JOIN table ON alias.column = table.column ...
– Raymond Nijland
Nov 16 '18 at 12:53
It is hard to suggest something solid without seeing table data but "Retrieve the companies that works in three different regions" That part you can easy get with
SELECT companies.company_name, COUNT(*) FROM works_in INNER JOIN companies ON works_in.company = company.company_id GROUP BY works_in.company HAVING COUNT(DISTINCT region_id) >= 3
and use that result in a subquery and JOIN that with other tables to get the other information you need.. something like this is possible in SQL.. SELECT * FROM ( SELECT ... ) AS alias INNER JOIN table ON alias.column = table.column ...
– Raymond Nijland
Nov 16 '18 at 12:53
Besides you can also write the
SELECT ...
subquery in a INNER JOIN
clause like this. SELECT * FROM table INNER JOIN ( SELECT ...) AS alias ON table.column = alias.column
.. Good luck with your SQL exercises and SQL studies.– Raymond Nijland
Nov 16 '18 at 12:58
Besides you can also write the
SELECT ...
subquery in a INNER JOIN
clause like this. SELECT * FROM table INNER JOIN ( SELECT ...) AS alias ON table.column = alias.column
.. Good luck with your SQL exercises and SQL studies.– Raymond Nijland
Nov 16 '18 at 12:58
|
show 2 more comments
1 Answer
1
active
oldest
votes
The final solution I could come up with is below, I try to go though the logical steps that took me to get there.
This is in T-SQL. (SQL Server 2016)
List how many regions a company works in
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
GROUP BY company_name
The result is like this (test data included below):
See which borders does a certain region have:
SELECT DISTINCT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = 1 OR region_id2 = 1)
List which adjacent regions a company is working in:
SELECT * from regions where exists (SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = regions.region_id OR region_id2 = regions.region_id)
AND w1.company_id = 4
AND w2.company_id = 4)
This is for company 4, this will be generalized later.
Putting it all together:
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
WHERE EXISTS (
SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = works_in.region_id OR region_id2 = works_in.region_id)
AND w1.company_id = companies.company_id
AND w2.company_id = companies.company_id
)
GROUP BY company_name
HAVING COUNT(works_in.region_id) = 3
This lists the companies that work in 3 adjacent regions. I left out selecting the actual results you need, you should be able to build on this and finish the exercise.
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337976%2fquery-about-companies-and-regions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The final solution I could come up with is below, I try to go though the logical steps that took me to get there.
This is in T-SQL. (SQL Server 2016)
List how many regions a company works in
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
GROUP BY company_name
The result is like this (test data included below):
See which borders does a certain region have:
SELECT DISTINCT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = 1 OR region_id2 = 1)
List which adjacent regions a company is working in:
SELECT * from regions where exists (SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = regions.region_id OR region_id2 = regions.region_id)
AND w1.company_id = 4
AND w2.company_id = 4)
This is for company 4, this will be generalized later.
Putting it all together:
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
WHERE EXISTS (
SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = works_in.region_id OR region_id2 = works_in.region_id)
AND w1.company_id = companies.company_id
AND w2.company_id = companies.company_id
)
GROUP BY company_name
HAVING COUNT(works_in.region_id) = 3
This lists the companies that work in 3 adjacent regions. I left out selecting the actual results you need, you should be able to build on this and finish the exercise.
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
add a comment |
The final solution I could come up with is below, I try to go though the logical steps that took me to get there.
This is in T-SQL. (SQL Server 2016)
List how many regions a company works in
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
GROUP BY company_name
The result is like this (test data included below):
See which borders does a certain region have:
SELECT DISTINCT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = 1 OR region_id2 = 1)
List which adjacent regions a company is working in:
SELECT * from regions where exists (SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = regions.region_id OR region_id2 = regions.region_id)
AND w1.company_id = 4
AND w2.company_id = 4)
This is for company 4, this will be generalized later.
Putting it all together:
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
WHERE EXISTS (
SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = works_in.region_id OR region_id2 = works_in.region_id)
AND w1.company_id = companies.company_id
AND w2.company_id = companies.company_id
)
GROUP BY company_name
HAVING COUNT(works_in.region_id) = 3
This lists the companies that work in 3 adjacent regions. I left out selecting the actual results you need, you should be able to build on this and finish the exercise.
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
add a comment |
The final solution I could come up with is below, I try to go though the logical steps that took me to get there.
This is in T-SQL. (SQL Server 2016)
List how many regions a company works in
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
GROUP BY company_name
The result is like this (test data included below):
See which borders does a certain region have:
SELECT DISTINCT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = 1 OR region_id2 = 1)
List which adjacent regions a company is working in:
SELECT * from regions where exists (SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = regions.region_id OR region_id2 = regions.region_id)
AND w1.company_id = 4
AND w2.company_id = 4)
This is for company 4, this will be generalized later.
Putting it all together:
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
WHERE EXISTS (
SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = works_in.region_id OR region_id2 = works_in.region_id)
AND w1.company_id = companies.company_id
AND w2.company_id = companies.company_id
)
GROUP BY company_name
HAVING COUNT(works_in.region_id) = 3
This lists the companies that work in 3 adjacent regions. I left out selecting the actual results you need, you should be able to build on this and finish the exercise.
The final solution I could come up with is below, I try to go though the logical steps that took me to get there.
This is in T-SQL. (SQL Server 2016)
List how many regions a company works in
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
GROUP BY company_name
The result is like this (test data included below):
See which borders does a certain region have:
SELECT DISTINCT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = 1 OR region_id2 = 1)
List which adjacent regions a company is working in:
SELECT * from regions where exists (SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = regions.region_id OR region_id2 = regions.region_id)
AND w1.company_id = 4
AND w2.company_id = 4)
This is for company 4, this will be generalized later.
Putting it all together:
SELECT company_name, COUNT(works_in.region_id) AS count FROM companies
LEFT JOIN works_in ON works_in.company_id = companies.company_id
WHERE EXISTS (
SELECT region_id1, region_id2 FROM region_borders
INNER JOIN works_in w1 ON region_borders.region_id1 = w1.region_id
INNER JOIN works_in w2 ON region_borders.region_id2 = w2.region_id
WHERE (region_id1 = works_in.region_id OR region_id2 = works_in.region_id)
AND w1.company_id = companies.company_id
AND w2.company_id = companies.company_id
)
GROUP BY company_name
HAVING COUNT(works_in.region_id) = 3
This lists the companies that work in 3 adjacent regions. I left out selecting the actual results you need, you should be able to build on this and finish the exercise.
answered Nov 16 '18 at 15:30
Marcell TóthMarcell Tóth
1,056317
1,056317
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
add a comment |
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
Thank you really a lot! I was getting crazy in understanding relations between bordering regions, but not only in SQL, just reasoning on a very normal table drawn with a pen on a sheet.
– user10662775
Nov 20 '18 at 16:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337976%2fquery-about-companies-and-regions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"Why should I provide an MCVE for what seems to me to be a very simple SQL query?" meta.stackoverflow.com/questions/333952/…
– Raymond Nijland
Nov 16 '18 at 12:48
3
Is that your homework?
– schlonzo
Nov 16 '18 at 12:50
Try combining
GROUP BY
withCOUNT DISTINCT
andHAVING
– schlonzo
Nov 16 '18 at 12:52
It is hard to suggest something solid without seeing table data but "Retrieve the companies that works in three different regions" That part you can easy get with
SELECT companies.company_name, COUNT(*) FROM works_in INNER JOIN companies ON works_in.company = company.company_id GROUP BY works_in.company HAVING COUNT(DISTINCT region_id) >= 3
and use that result in a subquery and JOIN that with other tables to get the other information you need.. something like this is possible in SQL..SELECT * FROM ( SELECT ... ) AS alias INNER JOIN table ON alias.column = table.column ...
– Raymond Nijland
Nov 16 '18 at 12:53
Besides you can also write the
SELECT ...
subquery in aINNER JOIN
clause like this.SELECT * FROM table INNER JOIN ( SELECT ...) AS alias ON table.column = alias.column
.. Good luck with your SQL exercises and SQL studies.– Raymond Nijland
Nov 16 '18 at 12:58