I was reading about the right outer join from tutorialspoint. I know that when a right outer join is performed the first thing that happens is an inner join of the two tables and then any rows in the right table that are missing in the left table are given null values.
Example from tutorial:
Customers table:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Orders table:
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
Query:
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Result:
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+------+----------+--------+---------------------+
Why is the result of the right outer join the same as the original right table? How is this right join in anyway useful? I see it as pointless.
This isn't a useful query for RIGHT JOIN. Since all orders should have a valid customer (in fact, there should be a foreign key relationship between the Order and Customers tables), there will never be an order with no matching customer, so you'll never get any null values added.
I think they included this query just to contrast it with the almost identical query on the LEFT JOIN and FULL JOIN pages of the tutorial. Those queries show all orders, as well as all customers that don't have any orders (they have NULL in the AMOUNT and DATE columns.
To get the equivalent result with RIGHT JOIN you can simply swap the order of the tables:
SELECT ID, NAME, AMOUNT, DATE
FROM ORDERS
RIGHT JOIN CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Because LEFT JOIN and RIGHT JOIN are equivalent like this, most programmers just use LEFT JOIN.
This is not the right example in that case.To see something which is there in orders table but not in customers table the id should be something that is not present in customers,such as 8,9 etc.
Related
I have following tables that manage revenue
revenue Table
+------------+--------+--------+
| revenue_id | amount | status |
+------------+--------+--------+
| 1 | 45000 | 1 |
| 2 | 25000 | 1 |
| 3 | 67000 | 1 |
| 4 | 22000 | 1 |
| 5 | 32000 | 0 |
+------------+--------+--------+
bank Table
+---------+--------+-------------+-------------+
| bank_id | ref_no | bank_amount | bank_status |
+---------+--------+-------------+-------------+
| 1 | 2 | 23000 | Pending |
| 2 | 3 | 67000 | Confirmed |
| 3 | 4 | 22000 | Confirmed |
+---------+--------+-------------+-------------+
02) If a revenue as mentioned in the revenue table has banked, it is recorded in the bank table. After that, the amounts in two tables are equal, the bank status may be into "Confirmed".
03) So, I need to get Confirmed records only as following by joining above two tables
Desired Output
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 3 | 67000 | 67000 | Confirmed |
| 4 | 22000 | 22000 | Confirmed |
+------------+--------+-------------+-------------+
Desired Output-02
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 1 | 45000 | | |
| 2 | 25000 | 23000 | Pending |
| 3 | 67000 | 67000 | Confirmed |
| 4 | 22000 | 22000 | Confirmed |
+------------+--------+-------------+-------------+
Desired Output-03
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 1 | 45000 | | |
| 2 | 25000 | 23000 | Pending |
+------------+--------+-------------+-------------+
04) To get the desired output I used the following query
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from revenue
left join bank on bank.ref_no = revenue.revenue_id
where revenue.status = 1 and bank.bank_status = "Confirmed"
05) But did't get the expected result. It generated only the empty result. I can not understand what I am going wrong. Can any one help me ?
You have to use Inner Join instead of Left JOIN for your expected output.
SELECT revenue.revenue_id,
revenue.amount,
bank.bank_amount,
bank.bank_status
FROM revenue
INNER JOIN bank ON bank.ref_no = revenue.revenue_id
WHERE revenue.status = 1
AND bank.bank_status = "Confirmed"
DEMO
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from revenue
Inner join (select *
From bank
Where bank_status = "Confirmed") bank
on bank.ref_no = revenue.revenue_id
You have to use the first bank table and then revenue table for left join .
Because in left join all rows will be returned even without matches.
So, use query like this.
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from bank
left join revenue on bank.ref_no = revenue.revenue_id
where revenue.status = 1 and bank.bank_status = "Confirmed"
I have a script which is working but not as desired. My aim is to select the most recently inputted record on the plans database for each seller in the account_manager_sellers list.
The current issue with the script below is: It is returning the oldest record rather than the newest, for example: it is selecting a record in 2016 rather than one which has a timestamp in 2018. (eventually I need to change the WHERE clause to get all lastsale records before 2017-01-01.
Simple Database Samples.
plans AKA (sales list)
+----+------------------+-----------+
| id | plan_written | seller_id |
+----+------------------+-----------+
| 1 | 20/09/2016 09:12 | 123 |
| 2 | 22/12/2016 09:45 | 444 |
| 3 | 19/10/2016 09:07 | 555 |
| 4 | 02/10/2015 14:26 | 123 |
| 5 | 15/08/2016 11:06 | 444 |
| 6 | 16/08/2016 11:03 | 123 |
| 7 | 03/10/2016 10:15 | 555 |
| 8 | 28/09/2016 10:12 | 123 |
| 9 | 27/09/2016 15:12 | 444 |
+----+------------------+-----------+
account_manager_sellers (seller list)
+-----+----------+
| id | name |
+-----+----------+
| 123 | person 1 |
| 444 | person 2 |
| 555 | person 3 |
+-----+----------+
Current Code Used
SELECT p.plan_written, p.seller_id
FROM plans AS p NATURAL JOIN (
SELECT id, MAX(plan_written) AS lastsale
FROM plans
GROUP BY seller_id
) AS t
JOIN account_manager_sellers AS a ON a.id = p.seller_id
WHERE lastsale < "2018-05-08 00:00:00"
Summary
Using the code and example tables above, this code would return these 3 results, whilst we do expect 3 results, the MAX(plan_written) does not seem to have followed, my guess is that it is something to do with the GROUP clause, I am not sure if we can utilise an ORDER BY and LIMIT clause?
+--------------+------------------+
| seller_id | plan_written |
+--------------+------------------+
| 123 | 16/08/2016 11:03 |
| 444 | 15/08/2016 11:06 |
| 555 | 03/10/2016 10:15 |
+--------------+------------------+
The join condition in your query is off, and you should be restricting to the max date for each seller. Also, you don't need to join to the account_manager_sellers table to get your expected output:
SELECT p1.*
FROM plans p1
INNER JOIN
(
SELECT
seller_id, MAX(plan_written) AS max_plan_written
FROM plans
WHERE plan_written < '2018-05-08 00:00:00'
GROUP BY seller_id
) p2
ON p1.seller_id = p2.seller_id AND
p1.plan_written = p2.max_plan_written;
i am watching a tutorial. There is a code which i don't understand what is supposed to do.
$sql = 'SELECT p.*,
a.screen_name AS author_name,
c.name AS category_name
FROM
posts p
LEFT JOIN
admin_users a ON p.author_id = a.id
LEFT JOIN
categories c ON p.category_id = c.id
WHERE
p.id = ?';
I read about the left joins but i didn't understand them. Can somebody please explain me the code i shared.
Thanks in advance!
Imagine you have two tables. One that stores the information about the programmers on your website, and the other table that keeps track of their online purchases.
PROGRAMMERS Table
+--------------------------------------------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Desire | 32 | 123 fake s| 3000.00 |
| 2 | Jamin | 25 | 234 fake s| 2500.00 |
| 3 | Jon | 23 | 567 fake s| 2000.00 |
| 4 | Bob | 30 | 789 fake s| 1500.00 |
| 5 | OtherGuy | 31 | 890 fake s| 1000.00 |
| 6 | DudeMan | 32 | 901 fake s| 500.00 |
+--------------------------------------------+
PURCHASES Table
+---------------------------------------------+
| ORDER_ID | PROG_ID | DATE | PRICE |
+-------------+---------+---------------------|
| 1 | 1 | 1-1-2017 | 100 |
| 2 | 2 | 1-2-2017 | 200 |
| 3 | 6 | 1-3-2017 | 300 |
+---------------------------------------------|
You decide you need to make a new table to consolidate this information to a table that contains
certain columns you want.
For example, you figure it would be nice for shipping purposes to have a table
that has the ID, the NAME, the PRICE, and the DATE columns.
Currently, the tables we have don't display all of that in a single table.
If we were to LEFT JOIN these tables, we would end up filling the desired columns
with NULL values where there is no information to join.
SELECT ID, NAME, PRICE, DATE
FROM PROGRAMMERS
LEFT JOIN PURCHASES
ON PROGRAMMERS.ID = PURCHASES.PROG_ID;
Notice that I'm selecting the columns I want from the starting table, then joining the right table
even though there might be missing information.
RESULTING TABLE
+-------------------------------------+
| ID | NAME | PRICE | DATE |
+----+----------+-----------------+---+
| 1 | Desire | 100 | 1-1-2017 |
| 2 | Jamin | 200 | 1-2-2017 |
| 3 | Jon | NULL | NULL |
| 4 | Bob | NULL | NULL |
| 5 | OtherGuy | NULL | NULL |
| 6 | DudeMan | 300 | 1-3-2017 |
+-------------------------------------+
For a visual representation of the difference between SQL JOINs check out
https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins .
I have 3 tables: NAMES, REGISTRATIONS, and RENEWALS. I'm using LEFT JOIN to join the 3 tables with a common ID.
I need to count the number of REGISTRATIONS of each user, as well as the number of RENEWALS. I've tried using different options in the GROUP BY field, but none seemed to work.
Here's the SELECT statement:
SELECT
names.name_id AS 'Names ID'
,names.name AS Name
,count(registrations.date) AS Registrations
,count(renewals.date) AS Renewals
FROM names
LEFT JOIN registrations
ON names.name_id = registrations.name_id
LEFT JOIN renewals
ON renewals.name_id = registrations.name_id
GROUP BY names.name_id, registrations.name_id, renewals.name_id;
And here are the 3 tables:
TABLE: names
+---------+------+
| name_id | name |
+---------+------+
| 1 | Ana |
| 2 | John |
| 3 | Paul |
+---------+------+
TABLE: registrations
+-----------------+---------+---------------------+-------+
| registration_id | name_id | date | value |
+-----------------+---------+---------------------+-------+
| 1 | 1 | 2014-01-30 13:15:02 | 15 |
| 2 | 2 | 2014-05-01 18:01:44 | 15 |
| 3 | 2 | 2014-07-08 15:10:43 | 20 |
| 4 | 3 | 2012-09-28 17:45:32 | 15 |
| 5 | 3 | 2014-01-09 18:26:14 | 20 |
| 6 | 3 | 2015-01-10 13:22:01 | 25 |
+-----------------+---------+---------------------+-------+
TABLE: renewals
+------------+---------+---------------------+-------+
| renewal_id | name_id | date | value |
+------------+---------+---------------------+-------+
| 1 | 1 | 2015-01-30 00:00:00 | 5 |
| 2 | 1 | 2016-02-12 00:00:00 | 5 |
| 3 | 1 | 2015-06-01 00:00:00 | 5 |
| 4 | 1 | 2013-11-24 00:00:00 | 5 |
| 5 | 2 | 2015-01-27 00:00:00 | 5 |
+------------+---------+---------------------+-------+
Here's the INCORRECT result I'm getting:
+----------+------+---------------+----------+
| Names ID | Name | Registrations | Renewals |
+----------+------+---------------+----------+
| 1 | Ana | 4 | 4 |
| 2 | John | 2 | 2 |
| 3 | Paul | 3 | 0 |
+----------+------+---------------+----------+
The CORRECT result I was expecting would be:
+----------+------+---------------+----------+
| Names ID | Name | Registrations | Renewals |
+----------+------+---------------+----------+
| 1 | Ana | 1 | 4 |
| 2 | John | 2 | 1 |
| 3 | Paul | 3 | 0 |
+----------+------+---------------+----------+
How can I fix the query to get a correct result?
Try this:
SELECT
names.name_id AS 'Names ID'
,names.name AS Name
,count(distinct registrations.registration_id) AS Registrations
,count(distinct renewals.renewal_id) AS Renewals
FROM names
LEFT JOIN registrations
ON names.name_id = registrations.name_id
LEFT JOIN renewals
ON renewals.name_id = registrations.name_id
GROUP BY names.name_id, registrations.name_id, renewals.name_id;
Whenever I run into this type of issue, I find it helps to just run a select * query if your server can take it. Like this:
SELECT *
FROM names
LEFT JOIN registrations
ON names.name_id = registrations.name_id
LEFT JOIN renewals
ON renewals.name_id = registrations.name_id ;
That will let you see what you are really counting.
Your query is executed just fine.
After the first join you have 1 entry for Ana, 2 entries for John and 3 for Paul.
After the seconds join the one entry for Ana is duplicated 4 times and joined (concatenated) with the 4 renewals. If you now count the registration dates for Ana you get 4. That is where your "errors" come from.
You could for example count the distinct dates to fix it.
I have 3 tables to join and need some help to make it work, this is my schema:
donations:
+--------------------+------------+
| uid | amount | date |
+---------+----------+------------+
| 1 | 20 | 2013-10-10 |
| 2 | 5 | 2013-10-03 |
| 2 | 50 | 2013-09-25 |
| 2 | 5 | 2013-10-01 |
+---------+----------+------------+
users:
+----+------------+
| id | username |
+----+------------+
| 1 | rob |
| 2 | mike |
+----+------------+
causes:
+--------------------+------------+
| id | uid | cause | <missing cid (cause id)
+---------+----------+------------+
| 1 | 1 | stop war |
| 2 | 2 | love |
| 3 | 2 | hate |
| 4 | 2 | love |
+---------+----------+------------+
Result I want (data cropped for reading purposes)
+---------+-------------+---------+-------------+
| id | username | amount | cause |
+---------+-------------+---------+-------------+
| 1 | rob | 20 | stop war |
| 2 | mike | 5 | love |
+---------+-------------+-----------------------+
etc...
This is my current query, but returns double data:
SELECT i.*, t.cause as tag_name
FROM users i
INNER JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.uid = tti.uid)
EDIT: fixed sql schema on fiddle
http://sqlfiddle.com/#!2/0e06c/1 schema and data
How I can do this?
It seems your table's model is not right. There should be a relation between the Causes and Donations.
If not when you do your joins you will get duplicated rows.
For instance. Your model could look like this:
Donations
+--------------------+------------+
| uid | amount | date | causeId
+---------+----------+------------+
| 1 | 20 | 2013-10-10 | 1
| 2 | 5 | 2013-10-03 | 2
| 2 | 50 | 2013-09-25 | 3
| 2 | 5 | 2013-10-01 | 2
+---------+----------+------------+
causes:
+----------------------+
| id | cause |
+---------+------------+
| 1 | stop war |
| 2 | love |
| 3 | hate |
+---------+------------+
And the right query then should be this
SELECT i.*, t.cause as tag_name
FROM users i
INNER JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.id = tti.causeId)
Try this
SELECT CONCAT(i.username ,' ',i.first_name) `name`,
SUM(tti.amount),
t.cause AS tag_name
FROM users i
LEFT JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.uid = tti.uid)
GROUP BY i.id
Fiddle
You need to match the id from both the users and causes table at the same time, like so:
SELECT i.*, t.cause as tag_name
FROM users i
INNER JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.uid = tti.uid and t.id = i.id)
Apologies for formatting, I'm typing this on a phone.