SQL query multiple tables in database - sql

I need to query 3 tables from a database.
The table Client includes client id no. and client name.
The table Property includes property id no. and property name amongst other things.
The table clientsInterestedInProperties includes client id no. property id no. and date of visit to property.
I want to list the name of clients who have an interest in a specific property (with the name, not id. no) and the date they visited the property.
For example say the property is called Barker Hall, who is interested in it and when did they visit?
Can anyone help?

First, use main filtering condition in your from table. So you must start with
select ... from Property ... ,
Then connect visitations to the property. It will generate different row for each visit, with
LEFT JOIN to get visitation to the property
Then we need to get readably client names, not IDs, so we do
LEFT JOIN to get client names
And some additional filtering/conditions can be added with
WHERE clauses ( or INNER JOIN ) , like visiting period.
Example SQL for your case ( i don't know your table/column names )
select PropertyName, ClientsInProp_VisitDate, ClientName
from Property
left join ClientsInProp on ClientsInProp_Propertyid = Property_PropertyID
left join Client on Client_Clientid = ClientsInProp_ClientId
where Proprty_Name = 'House 1' and ClientsInProp_VisitDate > '01.10.2013'
Cheers !

What you want is an IN clause:
SELECT * FROM Client
WHERE ClientId IN
(SELECT ClientID
FROM clientsInterestedInProperties cip
INNER JOIN Property p
ON cip.PropertyID = p.PropertyID
WHERE p.ProperyName = #propertyName)

Related

Join subquery with a count function

I have four tables:
Room (holds room details)
property( holds property details)
property interest (linking table matching customers with properties
interested in)
buying_potential_customer (customer details).
I currently have this query which tells me how many rooms in each property has.
SELECT
property.property_id,
property.property_address_first_line,
COUNT(room.property_id) AS number_of_rooms
FROM room
INNER JOIN property
ON room.property_id = property.property_id
GROUP BY property.property_id,
property.property_address_first_line
ORDER BY property.property_id;
The property_interest and buying_potential interest need to be joined to this also.
This is the property interest table
This is the buying potential customer table
In the end I need, the data from property interest which matches property with buyer but with the buyer's first and last name attached, additionally counting the number of rooms in the property which is the query above. It seems I would need a subquery but am not sure how to complete this, and help would be appreciated
Sorry I am quite new to SQL.
If I understand your aim :)
You must write a query as main table your middle table property_interest, so you can join it with others parent tables, as follow:
SELECT bpc.buying_customer_first_name || bpc.buying_customer_surname,
(SELECT COUNT(*)
FROM room r
JOIN property p
ON r.property_id = p.property_id
WHERE p.property_id = pi.property_id)
FROM property_interest pi
JOIN buying_potential_custoter bpc
ON pi.buying_customer_id = bpc.buying_customer_id

Scalar subquery contains more than one row

Im working with H2 database and wanted to move some data. For that I created the following Query:
UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID;
When I now perform it in the H2 console I get the following error:
Scalar subquery contains more than one row; SQL statement:
UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID [90053-192] 90053/90053 (Hilfe)
What is this error telling me?
EDIT
What I want to achiev with my query:
Actually every CUSTOMER has a CUSTOMER_SERVICE. And I simply want to move the COLUMN EMAIL from CUSTOMER_SERVICE to the CUSTOMER Table. for that I already added a email column to the user. I hoped to be able to do it with my query but obviously not.
Your query is not syntactically valid (all subqueries must have parentheses around them).
What you are missing is a correlation clause. I believe you want:
UPDATE CUSTOMER c
SET EMAIL = (SELECT cs.EMAIL
FROM CUSTOMER_SERVICE s
WHERE s.ID = c.CUSTOMER_SERVICE_ID
);
I don't know what this is supposed to be: [90053-192] 90053/90053 (Hilfe).
Your select query is returning more than one row. If you don't want it to, then you need to do something like an aggregate or LIMIT 1 or something similar.
Your sub-query for at least one of your customers has multiple email addresses.
You could ... (Select top 1 serverice.email ...
Or ... (Select max(serverice.email) ...
Update Customer Set EMail=B.Email
From Customer A
Join (Select ID,max(EMail) as EMail From CUSTOMER_SERVICE Group By ID) B
on (A.CUSTOMER_SERVICE_ID = B.ID)
I've spend a lot time with this error type (there shoudn't be duplicates in DB).
At last I've found problem via SQL with COUNT like this:
UPDATE CUSTOMER
SET EMAIL = SELECT COUNT(service.EMAIL)
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID =
customer.CUSTOMER_SERVICE_ID;
And then select problem rows with EMAIL!='1'

SQL Server View get count from related table where column = "True"

I have two related tables in SQL Server in a 1 to many relationship (Applicant, Reference).
I want a view that will retrieve all the data from the Applicant table and also add another column to the view that tells me how many related Reference rows there are where the "Complete" column is True.
Something like this using a subquery:
select applicantfield1, applicantfield2,
(select count(*) from
reference where reference.applicantkey = applicant.applicantkey
and reference.complete = 1) AS referencecount
from applicant
Unless the complete field is in the applicant table (not the reference table). If so, it would be more like this:
select applicantfield1, applicantfield2,
(select count(*) from
reference where
reference.applicantkey = applicant.applicantkey) AS referencecount
from applicant
where applicant.complete = 1
Something like the below. You need to join on a table. This will handle where the applicant also doesn't have a True reference.
SELECT A.*, isnull(r.comptotal,0) as CompleteTotal
FROM Applicant as a Left Join
(SELECT ApplicantId, Count(Complete) as comptotal
FROM Reference Where Complete=1 Group by ApplicantID) as r
on a.ApplicantId = r.applicantId

Creating a query that queries the data in a table more than once

I have a table of data of personal details of clients:
ID
name
addressID
referralID
Part of the data is a 'referralID'. This will be filled in for some entries if they have been referred by other clients in the database. Therefore the referralID will match up to an ID in the database.
I'm trying to create a query that will return the details of the referrers and the names of the people that they have referred e.g:
Referrals ID (Where referallID = ID),
Referrals name (name),
ID of client referred (ID),
Name of Client referred (name)
I'm having difficulty in how to approach this and what method to undertake as the query needs to reference itself in some way? How can i extract the details twice from the table?
Hope that is easy enough for someone to understand. Any help or guidance would be greatly appreciated, cheers
Replace MyTable with the name of your table and join to the same table using aliases:
SELECT
m.ID,
m.Name,
m.AddressID,
m2.name as ReferralName
FROM
MyTable m
INNER JOIN
MyTable m2
ON m2.ReferralID = m.ID
Assuming you want a list of all clients and the people they referred and that client is the table name and that ID=ReferallID...
Select * from client c1
LEFT join client c2 on C1.ID = C2.ReferallID

Why doesn't it work? A simple SQL query

I'm new to SQL, I have 3 tables in my database:
One named notifications,
the other notifications_log,
and the third one is control.
Both notification and notification_log have notification_id with pk-fk relation.
There is also another column named control_id in notifications and I have this column in control table too.
Now what I want to do is to get rows under description column of notification_log table by connecting notification table and control table with their control_id field. Can you help me with that?
Here's something I've tried:
select c.control_name
from notifications note, notifications_log note_log, control c
where note_log.ALARM_ID = note.ALARM_ID
and note.CONTROL_ID = C.CONTROL_ID
order by control_name desc
Use JOIN:
SELECT c.control_name, note_log.description FROM notifications note
INNER JOIN notifications_log note_log
ON note.notification_id = note_log.notification_id
INNER JOIN control c
ON note.control_id = c.control_id
ORDER BY c.control_name DESC
What you have to understand is if every record in control table has a corresponding record in other tables, otherwise it will not be showed with INNER JOIN.
select description from notification_log where notification_id in (select notification_id from notification)
if you want to get only the description..
hope it helps :D
From your description, it sounds as though you are selecting the wrong field in your query. Try:
select distinct note_log.description
from notifications note, notifications_log note_log, control c
where note_log.notification_id = note.notification_id
and note.CONTROL_ID = C.CONTROL_ID
order by 1