SQL - Select inside Select - sql

My skills with SQL are simple, so i'm not sure how to do this.. Here it goes.
Lets consider this.
Table A
ID Name LastNameID
1 Jonh 23
2 Helen 19
Table B
ID LastName
23 Bauer
19 Summers
.
SELECT ID Name LastNameID FROM TableA
How can i add another Select inside that one that will return the LastName from Table B based on the LastNameID in Table A.
In this case i usually use a simple function with the Programming language that i use to do that for each returned Row.
But i think its time to do that with SQL alone.
Any help is appreciated.

You just need to join the tables using the LastNameId field:
SELECT TableA.[Name] AS FirstName, TableB.LastName
FROM TableA
INNER JOIN TableB ON TableA.LastNameId = TableB.LastNameId

You are asking "how to do it with a select inside select". Usually, such queries are written in the form of joins, not as sub-selects. Doing joins is better than doing sub-selects for small number of tables.
The query for your application would be
SELECT firstNames.Name, lastNames.LastName FROM TableA firstNames, TableB lastNames
WHERE firstNames.LastNameId = lastNames.LastNameId;
This is the same as writing the query in an explicit join form as shown by Chris in his answer.

There are some good answers here, but if you actually have the first name in one table and last name in another table linked by an ID - I think it is time to review the basics. Start with looking at the Northwind database andmthen some of the database starter kits here.

Related

Can we select data from 2 tables in same time

From the code as shown below. I wonder that why it can select data from 2 tables in same time that are table "venue AS v" and table "as s".
or I misunderstand?
SELECT name, s.num_match
FROM venue AS v,
(SELECT venue_id, COUNT(*) AS num_match
FROM match
GROUP BY venue_id) AS s
WHERE v.venue_id = s.venue_id
LIMIT 3;
Yes you can using JOIN clause for example.
More infos here: https://www.informit.com/articles/article.aspx?p=30875&seqNum=5
Yes you can select data from as many as tables you want at the same time.
In this case you are trying to get an aggregated number from table-s and join it with the table v.
There are many ways to write the code to join the table. Above is one method which you have used.

Looking to display records from Table A that have more than one relationship to Table B

This sounds like a very simple query but I have never needed this calculation before. I'm using SQL Management Studio and SQL Server 2008.
I have a table ct_workers which contains individual employees and a second table cs_facilities which shows the sites that they work at.
The table ct_workers has a field person which is the primary ID for each employee and has a field facility which links the employees to cs_facilities via a field guid
I'm looking to display all workers that have 2 or more facilities.
I've though about using Excel or rownumber but surely that must be a simple efficient way of doing this?
Can anyone assist please?
Thanks,
You can use a GROUP BY with HAVING
SELECT cw.person
FROM ct_workers cw
GROUP BY cw.person
HAVING COUNT(DISTINCT cw.facility) >= 2
Your question suggests that you can use aggregation:
select w.person
from ct_workers w
group by w.person
having min(w.facility) <> max(w.facility); -- at least 2 values
However, if the person is the unique key in ct_workers, then a person can only be in one facility. So, your question would not make sense. You should actually have a junction table with one row per person and per facility.

SQL show difference between 2 tables

I know there are similar questions, but I can't find solution to what I need to do.
First of all I have 2 tables :
SD.Airlines (16k+ rows)
SD.AirlineRatings (405 rows)
I need to find which records form SD.AirlineRatings do I have in SD.Airlines, I did this :
SELECT b.Name AS Airline FROM SD.Airlines b
LEFT JOIN SD.AirlineRatings a ON a.AirlineName = b.Name
WHERE a.AirlineName IS NOT NULL;
Works fine, showed me 249/405 records. Now... If I need to compare those 249 records towards SD.AirlineRatings and check which ones I don't have.
I bet answer is simple but I don't know SQL that much.
Thanks
If you want records in AirLines ratings that are also in Airlines, then I would recommend EXISTS or IN:
select a.*
from sd.Airlines a
where exists (select 1 from sd.AirlineRatings ar where ar.AirlineName = a.Name);
If you want unrated airlines:
select a.*
from sd.Airlines a
where not exists (select 1 from sd.AirlineRatings ar where ar.AirlineName = a.Name);
And, if you want ratings on airlines that don't exist, you would swap the two tables in the query.

Is there a way to select automatically the row pointed by an FK on a given table?

Today while writing one of the many queries that every developer in my company write every day I stumbled upon a question.
The DBMS we are using is Sql Server 2008
Say for example I write a query like this in the usual PERSON - DEPARTMENT db example
select * from person where id = '01'
And this query returns one row:
id name fk_department
01 Joe dp_01
The question is: is there a way (maybe using an addon) to make sql server write and execute a select like this
select * from department where id = 'dp_01'
only by for example clicking with the mouse on the cell containing the fk value (dp_01 in the example query)? Or by right click and selecting something like ("Go to pointed value")?
I hope I didn't wrote something stupid or impossible by definition
Not really, but that seems like a silly thing to do. Why would you want to confuse an id with a department name?
Instead, you could arrange things so you could do:
select p.*
from person p
where department = 'dp_01';
You would do this by adding a computed column department that references a scalar function that looks up the value in the department table. You can read about computed columns here.
However, a computed column would have bad performance characteristics. In particular, it would basically require a full table scan on the person table, even if that is not appropriate.
Another solution is to create a view, v_person that has the additional columns you want. Then you would do:
select p.*
from v_person p
where department = 'dp_01';
Why can't you write yourself by saying
select * from department where id =
(select fk_department from person where id = '01')

MS Access Selecting Related Rows

I have 2 tables with a many-to-any relationship. For the example we will call the tables "Guys" and Girls" There is a junction table that contains the related primary keys...who has dated who.
If I want to find all the girls that Guy 1 has dated, I do a select on the junction table selecting all girls with guys.ID. This give me a RecordSet. Now to find the names of the girls, I need to select from the girls table a row using the key from each RecordSet row.
Isn't there an easier way? Since I've defined the relationships in Access I would think that there must be a way to build a single query. How do I do that?
SELECT girls.name
FROM (guys
INNER JOIN junct ON guys.guyID = junct.guyID)
INNER JOIN girls ON junct.girlID = girls.girlID
WHERE guys.guyID = [whatever id you're looking for]