Query table using inner join? - sql

We are trying to find the names of the customers who have both a loan and an account in the same branch. Should we use inner join here? So far we have only written;
select DISTINCT customer.name
FROM Customer, Has_Loan, Branch, Has_Account
WHERE
We have tried a few different things without getting any further, so appreciate any kind of help or hint:)

You can try this :
SELECT DISTINCT Customer.Name
FROM Customer
INNER JOIN Has_Account ON Customer.Ssn = Has_Account.Assn
INNER JOIN Account ON Account.AccountNo = Has_Account.ANo
INNER JOIN Has_Loan ON Customer.Ssn = Has_Loan.Lssn
INNER JOIN Loan ON Loan.LoanNo = Has_Loan.LNo
WHERE Loan.BranchID = Account.BranchID

select c.name
from customer c
join has_loan hl on hl.lssn = c.ssn
join loan l on hl.lno = l.loanno
join has_account ha on ha.assn = c.ssn
join account a on ha.ano = a.accountno
join branch b on b.branchid = a.branchid
where l.branchid = a.branchid
group by c.name;

Related

How can I connect multiple tables using an inner join?

I am trying to select and print customer_name(customer table), room_number(room table), room_rate (room table),rate_type(reservation table), checkin_date(reservation table), checkout_date(reservation table), billing_amount(billing table) from multiple tables. I tried to do an inner join but the error is 'column ambiguously defined '
SELECT *
FROM Customer c
INNER JOIN Reservation r
ON c.customer_id = r.customer_id
INNER JOIN Room s
ON s.room_id = r.room_id
INNER JOIN billing b
ON reservation_id = b.reservation_id
WHERE c.customer_name = 'John Scott';
You're missing the table alias on the left side of the last JOIN. Try this:
SELECT *
FROM Customer c
INNER JOIN Reservation r
ON c.customer_id = r.customer_id
INNER JOIN Room s
ON s.room_id = r.room_id
INNER JOIN billing b
ON r.reservation_id = b.reservation_id
WHERE c.customer_name = 'John Scott';
One problem is in your join and one in your select:
SELECT *
-------^ duplicate column names
FROM Customer c INNER JOIN
Reservation r
ON c.customer_id = r.customer_id INNER JOIN
Room s
ON s.room_id = r.room_id INNER JOIN
billing b
ON reservation_id = b.reservation_id
--------^ missing table alias
WHERE c.customer_name = 'John Scott';
If the only duplicate columns are the JOIN keys, then the USING clause (standard but not supported by all databases) is a handy way to get all the columns:
SELECT *
FROM Customer c INNER JOIN
Reservation r
USING (customer_id) INNER JOIN
Room s
USING (room_id) INNER JOIN
billing b
USING (reservation_id)
WHERE c.customer_name = 'John Scott';
When you using USING, the key columns are not duplicated with SELECT *.

How to make, count row result and become a column -MS ACCESS QUERY

SELECT DISTINCT Customers.Name, Customers.Stall_Number, Animal_Types.Code, Count(Animal_Types.Code) AS CountOfCode, Sum(Transaction_Details.Weight) AS TotalWeight
FROM (Customers INNER JOIN Transactions ON Customers.ID = Transactions.Customer) INNER JOIN ((Animal_Types INNER JOIN Carcass ON Animal_Types.ID = Carcass.Animal_Type) INNER JOIN Transaction_Details ON Carcass.ID = Transaction_Details.Carcass) ON Transactions.ID = Transaction_Details.Transaction
GROUP BY Customers.Name, Customers.Stall_Number, Animal_Types.Code;
Please refer to images below for more details.
To get rows to become columns, you will need to use a crosstab query:
TRANSFORM Count(Animal_Types.Code)
SELECT Customers.Name, Customers.Stall_Number, Sum(Transaction_Details.Weight) AS TotalWeight
FROM (Customers INNER JOIN Transactions ON Customers.ID = Transactions.Customer) INNER JOIN ((Animal_Types INNER JOIN Carcass ON Animal_Types.ID = Carcass.Animal_Type) INNER JOIN Transaction_Details ON Carcass.ID = Transaction_Details.Carcass) ON Transactions.ID = Transaction_Details.Transaction
GROUP BY Customers.Name, Customers.Stall_Number, Animal_Types.Code
PIVOT Animal_Types.Code

Join 4 tables with inner join

I'm trying to write below query with INNER JOIN instead of AND
select ph.name, ph.position, d.name, COUNT(app.appointmentId)
from physician ph, works_in w, appointment app, department d
where ph.eid = w.physician
and d.did = w.department
and ph.eid = app.physician
group by ph.eid, ph.name, ph.position, d.name
.
I tried in this way, But It gets so many errors
select ph.name, ph.position, d.name, COUNT(app.appointmentId)
from physician ph inner join works_in w
on ph.eid = w.department,
department d inner join works_in w
on d.eid = w.department,
physician ph inner join appointment app
on ph.ph.eid = app.physician
group by ph.eid, ph.name, ph.position, d.name
How can I write it correctly with inner joins.
First let me reffer you to this site that explain all about join syntax's
Change your query to this:
select ph.name, ph.position, d.name, COUNT(app.appointmentId)
from physician ph
inner join works_in w
on ph.eid = w.department
inner join department d
on d.eid = w.department
inner join appointment app
on ph.ph.eid = app.physician
group by ph.eid, ph.name, ph.position, d.name
The syntax for joins is :
SELECT <COLUMNS>
FROM <Table>
INNER JOIN <Another_Table>
ON(<Relations>)
INNER JOIN <Another_table2>
ON(<Other Relations>
SELECT ph.name AS Name, ph.position AS Position,d.name AS Name, COUNT(app.appointmentId) AS Appointment
FROM physician AS ph INNER JOIN works_in AS w
ON ph.eid = w.department INNER JOIN department d
ON d.eid = w.department INNER JOIN appointment app ON ph.eid = app.physician
GROUP BY ph.eid,ph.name,ph.position,d.name

Get one to many relation data

Hi everyone I am having three tables.
Customer(list of customers)
Payments(list of customer payment)
Orders(list of customer orders)
customer can have more than one payment and order that is one to many relation.
I tried following query but it is not showing proper result.
select a.name, b.job_date as JobDate, c.order_date as OrderDate from Customers a
inner join Jobs b on a.id = b.customer_id
inner join Orders c on a.id = c.customer_id
where a.id = 1;
What i need is to show a customer's orders and jobs.
Try left join instead of inner join. child tables might not have records assosiated with Customers.id =1.
select a.name, b.job_date as JobDate, c.order_date as OrderDate from Customers a
left join Jobs b on a.id = b.customer_id
left join Orders c on a.id = c.customer_id
where a.id = 1;
If you want to get all the orders you can use the following :
select a.name, c.order_date as OrderDate from Orders c
inner join Customers a on a.id = c.customer_id
where a.id = 1;
You get build a similar query to get all the employee's jobs:
select a.name, b.job_date as JobDate from Jobs b
inner join Customers a on a.id = b.customer_id
where a.id = 1;

INNER JOIN Distinct ID

I have the following code:
FROM CTE_Order cte
INNER JOIN tblOrders o
ON cte.OrderId = o.Id
INNER JOIN tblOrderUnits ou
ON o.id = ou.OrderId
INNER JOIN tblOrderServiceUnits osu
ON ou.VMSUnitID = osu.UnitId
When I join the ou I get 2 of the same unit Id's. This make the Inner Join tblOrderServiceUnits return 4 rows with 2 being duplicates. I need it to only return the 2 rows the are different. How do I use a distinct to Inner Join only distinct ou.id?
Sorry for the bad explanation but basically I am jsut trying to see how an INNER JOIN with a distinct subquery would work, If someone could give me an example of that I could figure it out from there.
INNER JOIN (SELECT DISTINCT * FROM X) Alias
ON Alias.ID = Primary.ID
For your example:
INNER JOIN (SELECT DISTINCT VMSUnitID, OrderId FROM tblOrderUnits) ou
ON o.id = ou.OrderId