SQL bring number of count, another column basis problem [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this table:
BANK - BRANCH_NAME - ADDRESS - DISTRICT - CITY - PHONE - FAX - OPENNING DATE
How do I bring branches on a city basis?
What I expect is a city knows how many branches it has.

From the comments:
what I expect is a city knows how many branches it has
You can use aggregation:
select city, count(*) no_branches from mytable group by city
If (city, branch) tuples are not unique in the table, then:
select city, count(distinct branch) no_branches from mytable group by city

Related

Hi guys i'm an SQL beginner and i'm having a hard time understanding it could u explain this line for me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
SELECT DISTINCT CITY FROM STATION WHERE MOD(ID,2)=0 ORDER BY CITY ASC;
SELECT DISTINCT CITY FROM STATION
Primary command to select data from the database. It is asking to select CITY from the table STATION whose data is unique. Thus, no duplicates are produced in result.
WHERE MOD(ID, 2) = 0
Only select those that have an even number ID.
ORDER BY CITY
Sort the results with respect to CITY names.
ASC
Sort in ascending order; which means that cities with names starting with A will come before those that have Z as first letter.

Why "WHERE" exists in SQL? Because "HAVING" can do its tasks plus more than that [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Why "WHERE" exists in SQL? Because "HAVING" can do its tasks plus more than that
Try using HAVING to do this query:
Sum the total sales per country for product 'XYZ'
SELECT country, SUM(amount)
FROM sales
GROUP BY country
HAVING product = 'XYZ'
No, that won't do it, because the groups will include sales for all products. And the HAVING condition is invalid because there are multiple products in each grouping.
The purpose of HAVING is to filter groups, after an aggregation is applied.
The purpose of WHERE is to filter rows, before an aggregation is applied.
SELECT country, SUM(amount)
FROM sales
WHERE product = 'XYZ'
GROUP BY country

sql view from two tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a table of Neighborhood that has primary key - NID and a table of Apartment that has a foreign key NeighborhoodID.
How do I create a view that show in each neighborhood how much apartments there.
Thank you!
I'd join the tables on the neighbourhood id, group by it, and count the rows:
CREATE OR REPLACE VIEW neighbourhood_apartments AS
SELECT n.name, COUNT(*)
FROM neighbourhood n
JOIN apartment a ON n.nid = a.neightbourhoodid
GROUP BY n.name
For each id in neighborhood count the rows in apartment that match the neighborhoodid:
SELECT
neighborhood.id,
neighborhood.name,
(SELECT COUNT(*) FROM apartment AS a WHERE a.neighborhoodid = n.id) AS counter
FROM
neighborhood AS n

compare one value with four tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to compare ssn with four tables to find any unmatched ssn. i need assistance with sql query. thanks in advance
Based on the information provided, the basic concept would be:
SELECT *
FROM yourTable
WHERE
ssn NOT IN (
SELECT ssn
FROM ssnTable1
)
AND
ssn NOT IN (
SELECT ssn
FROM ssnTable2
)
AND
ssn NOT IN (
SELECT ssn
FROM ssnTable3
)
AND
ssn NOT IN (
SELECT ssn
FROM ssnTable4
)
If the ssn should exist in all four tables at once, then replace the above ANDs with ORs.

making a sub-query FROM two tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
SELECT staffNO, name, surname, position
FROM Staff s, Branch b
WHERE s.branchNo = b.branchNo AND city = 'London';
I have tried to make the above SQL code into a sub-query and I'm not getting anywhere. Im a beginner in SQL, how do I go about it?
The equivalent query with a sub-query and IN clause would look like this:
SELECT staffNO, name, surname, position
FROM Staff s
WHERE s.branchNo IN (
SELECT b.branchNo
FROM Branch b
WHERE b.city = 'London'
);
This of course assumes that staffNO, name, surname, position are all available as fields on the Staff table. If any of those fields come from Branch then you do need to use the JOIN syntax instead.