DB2 SQL count in join or where clause - sql

This is probably an amateur question but I'm an amateur ! ;o)
I have several tables: Account table, Officer table, Location table, Web table...
The Officer table doesn't give me the number of officers per account.
I need to retrieve only the accounts that have more than 20 officers.
Should I use the COUNT ? If so, how ?
Your help is greatly appreciated.
Thanks.
Pat

Update:
select a.id, a.eff-date, l.address, l.city, c.phonenumber
from Account a
left outer join location l on a.id = l.id
left outer join contact c on a.id = c.id
where a.id in (
select a.id
from Account a
inner join Officer ao on a.id = ao.id
group by a.id
having count(*) > 20
)

Related

SQL QUERY ON 2 TABLES WITH EXCLUSION

I'm looking for a sql query to select IDs from the "product" table for "user" 1 that don't appear in the "Fake" table in the "product_id" field for "customer _id" 20 (for example). I hope to be clear enough in my request. Thanks for your help
Thanks XQbert. I think I succeeded thanks to your advice. Thank you
SELECT P.*, U.name
FROM product P
INNER JOIN user U
ON P.user_id = U.id
WHERE U.id= '1'
AND NOT EXISTS
(SELECT F.*
FROM fake F
INNER JOIN customer C
ON F.customer_id = C.id
WHERE P.id = F.product_id
AND C.customer_id = 20)

How to Join 3 Tabels in Sqlserver

I tried to join 3 tables all together but when I execute the query only the first row on the table is displayed. The table consists of many rows. How can I display multiple rows?
This is the code that I tried
SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
INNER JOIN member m
on l.memberid = m.id
INNER JOIN books b ON b.id = l.bookid
Member table
enter image description here
Books table
enter image description here
lend table
enter image description here
data i need to join
ID membername Bookname issue_date return_date
This is my first post.
It is possible that one of the other tables that you have joined to does not contain the other 2 records. In order to see all records from [lend], you would do this:
SELECT l.id, m.name, b.bookname, l.issuedate, l.returndate
FROM lend l
LEFT OUTER JOIN member m ON l.memberid = m.id
LEFT OUTER JOIN books b ON b.id = l.bookid
I am not sure which table contains the records that you are interested in, but select from only that table first. Check the record count. Then LEFT OUTER JOIN to one more table. Execute. Check record count. Keep going. That way you know which table join has affected the record count.
if you want all records of all tables:
SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
FULL OUTER JOIN member m
on l.memberid = m.id
FULL OUTER JOIN books b ON b.id = l.bookid
if you wand only records that are related with lend:
SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
left JOIN member m
on l.memberid = m.id
left JOIN books b ON b.id = l.bookid

SQL joins over more than two tables

I have 4 tables:
sales
products
vender_info
venders
Question is: display list of product_id whose vender and customer is different.
Please solve this question.
tables
Not sure what would you do with this data, but this is the query:
SELECT vp.PRODUCT_ID, vv.VENDERS, vs.CUSTOMERNAME
FROM vna_sales vs
JOIN vna_products vp ON vs.ORDERID = vp.ORDERID
JOIN vna_venders vv ON vp.VENDER_ID = vv.VENDER_ID
WHERE vv.VENDERS <> vs.CUSTOMERNAME
Please try this
SELECT *
FROM vna_shipments a
inner join vna_parts b on a.id = b.id //some joing factor
inner vna_suppliers c on a.id = c.id //some joing factor
inner vna_projects d on a.id = d.id //some joing factor
group by vendor_id, customer_id
order by vendor_id desc

SQL LEFT JOIN for joining three tables but one with to exclude content

I have 3 tables
STUDENTS
FEES_PAID
SUSPENDED
I want to get the details of the students who have paid the fees but not from SUSPENDED.
SELECT
ID
FROM
STUDENTS s
LEFT JOIN
SUSPENDED p ON s.ID = p.ID
INNER JOIN
FEES_PAID f ON f.ID = s.ID
WHERE
s.ID IS NULL
Unfortunately this does not work. Can any one suggest an efficient query?
Thanks in advance
You need to check if the second table is missing from the LEFT JOIN. So, you need to look at a column in that table. Change the WHERE to:
WHERE p.ID IS NULL
Alternatively, use NOT EXISTS:
SELECT s.ID
FROM STUDENTS s INNER JOIN
FEES_PAID f
ON f.ID = s.ID
WHERE NOT EXISTS (SELECT 1 FROM SUSPENDED p WHERE s.ID = p.ID);
Note that for both these queries, you will need to qualify the ID in the SELECT to specify the table where it comes from.
This should work:
SELECT
s.ID
FROM
STUDENTS s
LEFT JOIN
SUSPENDED p
ON s.ID=p.ID
INNER JOIN
FEES_PAID f
ON f.ID= s.ID
WHERE
p.ID IS NULL

Oracle Join across tables to count 'outstanding invoices'

I'm trying to find "the number of outstanding invoices for a given contract rate".
SQL Fiddle here (updated with ujjwal's answer to add a distinct)
You'll notice I have 350 outstanding invoices which is a lot more than the 14 I expected so I guess I haven't restricted the INNER JOIN's correctly...
Any ideas would be great thanks (sorry I'm rusty on SQL), this is for the latest version of Oracle.
Wayne
Can you try selecting only distinct entries:
SELECT
r.id AS rate_id,
COUNT(distinct mi.id) AS outstanding_invoices **change here**
FROM
contract_rate r
INNER JOIN
contract c ON r.contract_id = c.id
INNER JOIN
contractor con ON con.id = c.contractor_id
INNER JOIN
maintenance_item item ON item.contract_rate_id = r.id
INNER JOIN
maintenance_invoice mi ON mi.contractor_id = c.contractor_id AND mi.status = 'Awaiting Approval'
GROUP BY
r.id