I was preparing for exam and I have this exercise that I don't understand
I have table of Clients that have ClientID,
and also I have table of Transactions that have Foreign Key referenced to Clients, SenderID and RecieverID (refering to ClientID)
I need to create view that will show Transactions with Sender name and Reciever Name, and I did it but I don't understand how it works and why
Here is code:
SELECT CS.Name [SenderName], CR.Name [RecieverName]
FROM Transactions T
INNER JOIN Clients CS
ON CS.ClientID = T.SenderID
INNER JOIN Clients CR
ON CR.ClientID = T.RecieverID
Each time you need the name (for sender or recevier ) you need a relation based on the key between the the transaction table and the clients table
you need the name of the sender ( first join with Clients ) and the name for recevier ( second join with Clients )
for avoid confusion between the two (same name) table you need an alias that let you join the specific related tabe .. you use CS and CR as table nale alias
in this way is as you work with two differente table name (or with a logical duplication of the same table)
SELECT CS.Name [SenderName], CR.Name [RecieverName]
FROM Transactions T
INNER JOIN Clients CS ON CS.ClientID = T.SenderID
INNER JOIN Clients CR ON CR.ClientID = T.RecieverID
You can thinks at the table content as a set of data ..so you use two time the same set of data extracting the row mathcing your relation each time.
Each row in the table Transactions contains:
a SenderID which points to a row in the table Clients and
a RecieverID which points to another row in the table Clients.
So you must make one join of Transactions to Clients using SenderID to get the sender's name and another join to Clients using RecieverID to get the reciever's name.
Related
I have three tables in sql.
transcation_table: id(pk), date_time, sender_wallet(fk), amount_sent, sender_updated_balance, receiver_wallet(fk), amount_received, receiver_updated_balance
wallet_table: id(pk), userid(fk), wallet, currency, balance
user_table: id(pk), uname
all 'id' fields are primary keys
wallet_table.userid is foreign key of user_table.id
transaction_table.sender_wallet is foreign key of wallet_table.id
transaction_table.receiver_wallet is foreign key of wallet_table.id
I am trying to ask the database to give me a new table where any of the records in transaction_table contain a sender_wallet and receiver_wallet for a particular user.
The new returned table should be like:
date_time, sender_uname, amount_sent, sender_updated_balance, receiver_uname, amount_received, receiver_updated_balance
sender_uname and receiver_uname are new columns to be created for the purpose of making a distinction between the sender and receiver username in the newly returned table.
A returned result would be something like:
2023-02-03 09:57:38, marvin381, 40.00, 360.00, hamarni242, 40.00, 440.00
I made some poor attempts at trying to receive the intended result.
I am unable to see how I can pull the uname from the wallet_table effectively twice and join to the new table.
I also am not getting close to creating the new columns 'sender_uname' and 'receiver_uname'.
I have managed to get the inner join to work getting the data but with only one uname, but not the uname twice under 'sender' and 'receiver'.
which is not even working or coming close to the result.
select t.id,
t.date_time,
su.uname as sender_uname,
t.amount_sent,
t.sender_updated_balance,
ru.uname as receiver_uname,
t.amount_received,
t.receiver_updated_balance
from transcation_table t
inner join wallet_table sw on t.sender_wallet = sw.id
inner join wallet_table rw on t.receiver_wallet = rw.id
inner join user_table su on su.id = sw.userid
inner join user_table ru on ru.id = rw.userid
where su.id = x or ru.id = x;
That x would be a parameter (or a constant) written depending on your database which you didn't specify in tags.
PS: You could also join wallet_table and user table once with a CTE but I didn't want to go that route without knowing your database.
Here is a DBFiddle sample
I have four tables where I am trying to left join the 2nd-4th to the one on the left in this picture. From left to right:
1st table (jobs) is a table of jobs
2nd table (applications_jobs) is a bridge table to link jobs and application IDs
3rd table (applications) is applications
4th table (candidates) is candidates based on those applications
I want to get some columns from 1st table (jobs) and 4th table (candidates). I want to get job name (name) and status (status) columns from jobs table. I want to get first name (first_name) and last name (last_name) from candidates table.
Here's what I've tried:
SELECT
name, status, first_name, last_name
FROM
jobs, candidates
left join
applications_jobs aj on jobs.job_id = id
left join
applications a on aj.job_id = a.id
left join
candidates c on a.candidate_id = c.id
but get a error:
ERROR: invalid reference to FROM-clause entry for table "applications_jobs"
HINT: There is an entry for table "applications_jobs", but it cannot be referenced
from this part of the query.
any ideas?
The items you are selecting should be identified by the table they are coming from when you are performing joins, though it is not always necessary if the tables don't share column names. By writing it out, it would help to prevent the confusion you're having with the FROM clause.
The FROM clause can only be from a single table. In this case, it would be your 'jobs' table. Also, to properly reference your columns in your query, the first join should be application_jobs aj ON aj.job_id = jobs.id, and your second join should be applications a ON aj.application_id = a.id.
SELECT
"jobs".name, "jobs".status, "c".first_name, "c".last_name
FROM
jobs
left join
applications_jobs aj on aj.job_id = jobs.id
left join
applications a on aj.application_id = a.id
left join
candidates c on a.candidate_id = c.id
If you are still getting NULLs for the first and last names, Then you don't have candidates that have applications for that specific job. If you want to omit results that would otherwise be NULL, you can do an INNER JOIN on candidates so that it only returns records that exist on both sides of the equation.
I'm just starting in the SQL world, so I have a very noob question:
I have 2 tables:
clients (columns: client_id and name)
accounts (columns: account_id and client_id)
and I need to write a query that shows the accounts of all the clients.
But, the problem is that not all the clients have accounts, if the client doesn't have one: how can I show the client_id, the name and NULL for the account_id column?
This query should work:
SELECT *
FROM accounts
LEFT [OUTER] JOIN clients
ON accounts.client_id = clients.client_id;
if not try this one:
SELECT *
FROM accounts
LEFT [OUTER] JOIN clients
ON accounts.client_id = clients.client_id WHERE clients.client_id IS NOT NULL;
These are plain SQL queries, I mean they are not PL-SQL specific. LEFT [OUTER] JOIN will only returns the columns of accounts table. [OUTER] keyword is optional, it defers from database version to version. ON accounts.client_id = clients.client_id will match client_id columns in both tables. Lastly, WHERE clients.client_id IS NOT NULL part should prevent the rows with NULL values in client_id cells.
Useful link: https://www.techonthenet.com/oracle/joins.php
Try this query it returns the clients name client id and shows null to those client who has no accountid.
select clients.name, accounts.account_id from accounts left join clients on
accounts.clintid=clients.client_id
Let's say that I have a table called Appointments, another called Clients and a joining table called ClientsAppointments which relates the two aforementioned ones. This is used in a scenario where a client can have several appointments, and several clients can attend to the same appointment (n-n relation).
I would like to list the Appointments with a field "clients" being an array of all the Clients related to that appointment. What I've tried so far:
SELECT * FROM Appointments a
INNER JOIN ClientsAppointments ca ON ca.IdAppointment = a.IdAppointment
INNER JOIN Clients c ON c.IdClient = ca.IdClient
That doesn't work, of course. It gives me a list of appointments repeated with each of the clients they have. Then in PHP I would process them to achieve this. It seemed more efficient this way rather than making multiple queries because there's usually only one client per appointment.
Table schema (these are not the actual tables, but simplified to illustrate the case):
Appointment(
INT idAppointment,
DATETIME start,
DATETIME end)
Clients(
INT idClient,
VARCHAR name)
ClientsAppointments(
INT idAppointment,
INT idClient)
If you use MySQL as a database try to use GROUP_CONCAT:
SELECT a.IdAppointment,GROUP_CONCAT(CAST(ca.IdClient AS CHAR) SEPARATOR ',') FROM Appointments a
INNER JOIN ClientsAppointments ca ON ca.IdAppointment = a.IdAppointment
group by a.IdAppointment
Try to use LEFT OUTER JOIN
SELECT a.*,c.Name FROM ClientsAppointments ca
LEFT OUTER JOIN Appointments a ON ca.IdAppointment = a.IdAppointment
LEFT OUTER JOIN Clients c ON ca.IdClient = c.IdClient
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