I want to select all rows of the table PERSON that has no records into DOCUMENT table that have the primary table as FK.
What could be the better for me? LEFT JOIN? NOT IN? Any other solution?
Here's the simple scheme:
PERSON:
personId
personName
personSex
DOCUMENT:
documentId
FK_Person
Thank you in advance
Use a left join and check if the link to the other table failed (is null)
select p.*
from person p
left join document d on p.personId = d.fk_person
where d.fk_person is null
See this explanation of joins
try using not exist or left join :
select P.*
from PERSON P left join DOCUMENT D on P.personId = D.FK_Person
where D.FK_Person is null
OR
select * from PERSON P
where not exists (select 1 from DOCUMENT D where P.personId = D.FK_Person)
Related
select student_surname,
student_name,
student_recordbook,
student_kurs,
student_state,
student_dep_id,
student_kafedra_id
from
student
where student_studgroups = (select studgroups_number
from studgroups
join study on study_studgroups_id = studgroups_id
where studgroups_year != study_kurs
and study_state_id IN (1,2,3,4,5,6,7,21,22,23,24));
I need to optimize it without a subquery.
Thanks.
You can try like below-
select * from student a
inner join student_groups b on a.student_studgroup=b.studgroups_number
inner join study on study_studgroup_id=stud_Group_id
where (your condition --)
Please try this..
select *
from student s
join studgroups sg on (s.studygroups = sg.studgroups_number)
join study st on (sg.studgroupys_id = st.study_studgroups_id)
where sg.studgroups_year !=s.study_kurs
and s.study_state_id IN (1,2,3,4,5,6,7,21,22,23,24);
What I did here, I joined all three tables with the help of primary and foriegn key.
So you can get the same result using JOIN what you get from using subquery.
Table 1: Building
ProjectNO (FK)
BuildingNO
Floors
location
Table 2: Project
ProjectNO (PK)
ProjectName
CityName
I need to join "project" and "Building" because I need the common Buildings in Project and Building by the key ProjectNO.
thank you
you should use inner join keyword to get matching records from both tables.
If you have 2 tables with the same ProjectNO to be joined,
select * from Project p
inner join
Building b
on p.ProjectNO = b.ProjectNO;
You seems want :
select b.*
from Building b
where exists (select 1 from Project p where p.ProjectNO = b.ProjectNO);
If you want ProjectName, CityName then you can do JOIN :
select p.*, b.*
from Project p inner join
Building b
on p.ProjectNO = b.ProjectNO;
you should do inner join to get matching record from both tables like follows...
SELECT * FROM Building INNER JOIN Project ON Project.ProjectNO=Building.ProjectNO;
it will return only those records whose ProjectNO exists in both tables.
just use inner join because The INNER JOIN keyword selects records that have matching values in both tables.
select p.*, b.ProjectName
from Project p inner join
Building b
on p.ProjectNO = b.ProjectNO;
I have an SQL question. It's a simple problem, but I'm not an SQL guy at all.
Here is the situation, I have three tables:
CUSTOMER(
PK(customer_id)
)
LOAN(
PK(loan_id),
customer_it,
behavior_id
)
BEHAVIOR(
PK(behavior_id),
unpaid_number
)
// PK(x): x is a primary key.
I would like to select all of the CUSTOMERs who have an unpaid_number >= 1.
Can anybody show me a way to work this around?
Thanks
You are looking for INNER JOIN. Use like:
SELECT * FROM CUSTOMER c
INNER JOIN LOAN l ON c.customer_id = l.customer_it
INNER JOIN BEHAVIOR b ON b.behavior_id = l.behavior_id
WHERE b.unpaid_number>=1
Use inner join
SELECT c.* FROM CUSTOMER c INNER JOIN LOAN l ON l.customer_id = c.Customer_id INNER JOIN BEHAVIOR b ON b.behavior_id = l.behavior_id WHERE unpaid_number >=1
Actually, if you want all customers, you presumably want one row per customer, regardless of the number of matching rows in behavior.
That would suggest using exists or in:
select c.*
from customer c
where exists (select 1
from loan l join
behavior b
on b.behavior_id = l.behavior_id
where b.unpaid_number >= 1 and
l.customer_id = c.customer_id
);
This is particularly important if you are considering using select distinct.
Please, try below code
SELECT c.*
FROM CUSTOMER c
INNER JOIN LOAN l
ON l.customer_id = c.Customer_id
INNER JOIN BEHAVIOR b
ON b.behavior_id = l.behavior_id
WHERE unpaid_number >=1
try this?
SELECT LOAN.customer_it FROM LOAN
WHERE LOAN.behavior_id IN
(SELECT BEHAVIOR.behavior_id
from BEHAVIOR where BEHAVIOR.unpaid_number>=1)
I have a table called Doctor.
Doctor table consist of 4 fields. DoctorID,DoctorName, DoctorAddress, DoctorSpeciality
I have another table called PatientData. and it has 4 fields in it. PatientId, PatientName, PatientTelephone, DoctorID.
I need to write a SQL that would display the following fields;
PatientID, PatientName, DoctorName, DoctorSpeciality
1.) I think, i will have to use an INNER JOIN here, but i am not sure how to write it for this scenario. An outer join would also work i guess, but i am new to Joins. Can someone please help me here ?
2.) Can i create a VIEW for the SQL statement that i am creating above ?
Something like this should would using a regular INNER JOIN -- this will return all records from the Doctor table with a matching record in the PatientData table:
SELECT PD.PatientId, PD.PatientName, D.DoctorName, D.DoctorSpecialty
FROM Doctor D
INNER JOIN PatientData PD ON D.DoctorId = PD.DoctorId
If you want to return all data from one of the other tables, look into using a OUTER JOIN (I prefer LEFT JOINs).
Here's a nice article on visual representation of joins: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
And yes, you can create a view if you'd like -- depends on your needs. Something like this should be close:
CREATE VIEW DoctorPatients AS
SELECT PD.PatientId, PD.PatientName, D.DoctorName, D.DoctorSpecialty
FROM Doctor D
INNER JOIN PatientData PD ON D.DoctorId = PD.DoctorId
SQL Server Views: http://msdn.microsoft.com/en-us/library/ms187956.aspx
It is a simple join
SELECT p.PatientID, p.PatientName, d.DoctorName, d.DoctorSpeciality
FROM PatientData p JOIN Doctor d on d.DoctorID = p.PatientID
Of course you could create a view from this
CREATE VIEW [dbo].[PatientAndDoctor]
AS
SELECT p.PatientID, p.PatientName, d.DoctorName, d.DoctorSpeciality
FROM PatientData p JOIN Doctor d on d.DoctorID = p.PatientID
I have three tables:
COLLECTION
PERSON
PERSON_COLLECTION
where PERSON_COLLECTION is a mapping table id|person_id|collection_id
I now want to select all entries in collection and order them by person.name.
Do I have to join the separate tables with the mapping table first and then do a join again on the results?
SELECT
c.*,
p.Name
FROM
Collection c
JOIN Person_Collection pc ON pc.collection_id = c.id
JOIN Person p ON p.id = pc.person_id
ORDER BY p.Name
Not sure without the table schema but, my take is:
SELECT
c.*,
p.*
FROM
Person_Collection pc
LEFT JOIN Collection c
ON pc.collection_id = c.id
LEFT JOIN Person p
ON pc.person_id = p.id
ORDER BY p.name
The order you join won't break it but depending on which sql product you're using may effect performance.
You need to decide if you want ALL records from both/either table or only records which have a matching mapping entry, this will change the type of join you need to use.