making a sub-query FROM two tables [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 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.

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.

SQL bring number of count, another column basis problem [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
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

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.

find student name less than 50 attendance [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 8 years ago.
Improve this question
I have a student table where column is name, attendance, and date in which data is entered everyday for the students who attend the class. For example if a student is absent on a day, entry is not made for that particular student for that day.
Finally. I need to find out students name whose attendance less than 50.
You can use GROUP BY and HAVING statements for this.
SELECT name FROM student GROUP BY name HAVING COUNT(*) < 50;
Please note that above query is not tested.
You have to use GROUP BY clause to aggregate similar student in table and HAVING check your condition, to get your desired output.
SELECT name, count(name)
FROM student
GROUP BY column_name
HAVING count(name)<50;
I hope this will help to solved your problem.
SELECT name
FROM StudentsTable
WHERE COUNT(name) < 50