What happens if I select two tables with no WHERE clause? - mysql

I had a technical interview last week, and my interviewer asked me what happens if I run the following query:
SELECT * FROM tbl1, tbl2
I think I answered it correctly, but it wasn't an in-depth answer.
I said that I would select all the columns in both tables. For example if tbl1 has 3 columns, and tbl2 has 4 columns. The result set would have 7 columns.
Then he asked me why 7? and I said because I was selecting everything from each table.
That was a bad answer, but I couldn't think of anything else.
To cut to the chase, after the interviewed I executed the latter statement using two tables.
Table A, had 3 animal: dog, cat and elephant.
Table B had 2 names: Mat and Beth
This is the result set that I got after the statement being executed:
*********************************************
| id_tbl1 | name_tbl1 | id_tbl2 | name_tbl2 |
*********************************************
| 1 | dog | 1 | Mat |
| 2 | cat | 1 | Mat |
| 3 | elephant | 1 | Mat |
| 1 | dog | 2 | Beth |
| 2 | cat | 2 | Beth |
| 3 | elephant | 2 | Beth |
*********************************************
So my question is, why does the statement behaves like that?
In other words:
Why does the Table B's records repeat themselves until I reach the end of table A, and then it starts all over again?
How would you have answered the question in a way that it would've "WOW'd" the interviewer?
If this question does not belong to SO, feel free to delete it or close it!

If you do a select like this, all rows in one resultset are joined to all rows in the other resultset (Cartesian Product).
So you get a list of all rows of the first table with the first row of the second table, Then all entries for the second row and so on. The order may be an implementation detail. Not sure if it is defined that the first order is by the first table, it might be different across implementations.
If you join three tables (or more), then the same happens with all rows of all tables. This, of course, is not only for tables, but for any result set from joins.

The result will be a cartisian product
take a look at this example
SQL Example
You can see there are two tables one has 5 records and the other has 4 and the result is 20 records. Means 5 * 4 = 20 instead of 5 + 4 = 9 as you are assuming.
Table1
| IDX | VAL |
---------------
| 1 | 1val1 |
| 1 | 1val2 |
| 2 | 2val1 |
| 2 | 2val2 |
| 2 | 2val3 |
Table2
| ID | POINTS |
---------------
| 1 | 2 |
| 2 | 10 |
| 3 | 21 |
| 4 | 29 |
Result of below query
SELECT * FROM Table1 , Table2
| IDX | VAL | ID | POINTS |
-----------------------------
| 1 | 1val1 | 1 | 2 |
| 1 | 1val1 | 2 | 10 |
| 1 | 1val1 | 3 | 21 |
| 1 | 1val1 | 4 | 29 |
| 1 | 1val2 | 1 | 2 |
| 1 | 1val2 | 2 | 10 |
| 1 | 1val2 | 3 | 21 |
| 1 | 1val2 | 4 | 29 |
| 2 | 2val1 | 1 | 2 |
| 2 | 2val1 | 2 | 10 |
| 2 | 2val1 | 3 | 21 |
| 2 | 2val1 | 4 | 29 |
| 2 | 2val2 | 1 | 2 |
| 2 | 2val2 | 2 | 10 |
| 2 | 2val2 | 3 | 21 |
| 2 | 2val2 | 4 | 29 |
| 2 | 2val3 | 1 | 2 |
| 2 | 2val3 | 2 | 10 |
| 2 | 2val3 | 3 | 21 |
| 2 | 2val3 | 4 | 29 |

I think you are confusing yourself by running an example with two tables that have identical fields. You are referring to a Union, which will combine the values of 1 table with another, and using your example this would give you 3 + 4 = 7 results.
The comma separated FROM statement is doing JOIN, which will go through all values in Table X and pair them with all the values of Table Y. This would result in Size of X * Size of Y results, and using your example this would be 3 * 4 = 12.

Related

SQL - Aggregate all EXCEPT group

Consider the following sample table from a soccer tournament (let's call this table matches)
+----------+---------+--------------+
| match_id | club_id | goals_scored |
+----------+---------+--------------+
| 1 | 1 | 1 |
| 1 | 2 | 0 |
| 2 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 1 | 0 |
| 3 | 4 | 2 |
| 4 | 2 | 2 |
| 4 | 3 | 4 |
| 5 | 2 | 4 |
| 5 | 4 | 0 |
| 6 | 3 | 1 |
| 6 | 4 | 1 |
+----------+---------+--------------+
The resulting table we want should give us each club's total goals scored AND goals conceded:
+---------+--------------+----------------+
| club_id | goals_scored | goals_conceded |
+---------+--------------+----------------+
| 1 | 2 | 4 |
| 2 | 6 | 4 |
| 3 | 6 | 4 |
| 4 | 3 | 5 |
+---------+--------------+----------------+
Getting goals scored is straight forward enough...
SELECT SUM(goals_scored),
club_id
FROM matches
GROUP BY club_id
but I am absolutely flummoxed as to how to get it for each team's opponents.
I could, of course, construct a pretty complex array of subqueries to get there. If this were application-side work I'd likely just stuff it in a loop and iterate over each club to get there, but my use case requires a SQL answer if possible. Any thoughts?
edit: also if anyone has any better ideas on how to title this question, I'm all ears - I'm not really sure exactly how to describe this problem in the first place.
We can use a self-join approach here:
SELECT
m1.club_id,
SUM(m1.goals_scored) AS goals_scored,
SUM(m2.goals_scored) AS goals_conceded
FROM matches m1
INNER JOIN matches m2
ON m2.match_id = m1.match_id AND
m2.club_id <> m1.club_id
GROUP BY
m1.club_id
ORDER BY
m1.club_id;
This approach brings the goals conceded by each club to the other club, for each match, into a single row. We then just aggregate by club to get the two sums.

How to select every level with different user and user progress

I wanted to make a learning apps with a completed questions progress track on each level. I have 2 tables which contain all the level with its question count, and a table that save an user progress (completed questions) on each level. Here is the record,
Level table:
+----------+----------------------+-------------+--------+
| id_level | name | jumlah_soal | id_sub |
+----------+----------------------+-------------+--------+
| 1 | Basic Level 1 | 5 | 1 |
| 2 | Basic Level 2 | 6 | 1 |
| 3 | Basic Level 3 | 7 | 1 |
| 8 | Intermediate Level 1 | 5 | 2 |
| 9 | Intermediate Level 2 | 5 | 2 |
| 10 | Intermediate Level 3 | 5 | 2 |
..........................................................
Progress Table:
+-----------------+---------+-------------+----------+
| id_progreslevel | id_user | completed | id_level |
+-----------------+---------+-------------+----------+
| 1 | 1 | 2 | 1 |
| 2 | 1 | 3 | 2 |
| 3 | 2 | 2 | 1 |
+-----------------+---------+-------------+----------+
and when I joined the tables with the following query
SELECT IFNULL(progreslevel.id_user, 1) as id_user,
-> level.*, IFNULL(progreslevel.completed, 0) as completedquestions
-> FROM level LEFT JOIN progreslevel
-> ON level.id_level = progreslevel.id_level
-> WHERE level.id_sub = 1
-> HAVING id_user = 1;
It queries what I wanted:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 1 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 1 | 2 | Basic Level 2 | 6 | 1 | 3 |
| 1 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
BUT, when I tried to change to query a progress for user ID = 2, it became like this:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 2 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 2 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
Yes, the Basic Level 2 is gone because user 2 hadn't done it yet but user 1 had.
This is where I'm stuck, I want to whichever user I choose, its always query all the level, even when the other user had done it. It should be like this:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 2 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 2 | 2 | Basic Level 2 | 6 | 1 | 0 |
| 2 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
How do I achieve this?
Thanks in advance and Sorry if you got dizzy either at my explanation or database, I tried my best to translate it, so it's understandable
I'm not really sure that I understand the requirement, but I think you're after something like this...
SELECT l.*
, 2 id_user
, COALESCE(MAX(p.completed),0) completed_questions
FROM level l
LEFT
JOIN progress p
ON p.id_level = l.id_level
AND p.id_user = 2
WHERE l.id_sub = 1
GROUP
BY l.id_level;

SELECT OTHER TABLE

I have a little bit issue about MYSQL query
So I have table named "Diagnose" and "Diagnose Master"
in "Diagnose" there is an related field & record
idd | idmed | idf1 | idf2 | idf3
1 | 20 | 5 | 8 | 9
2 | 21 | 3 | - | 11
3 | 22 | 7 | 1 | -
4 | 23 | 1 | - | -
5 | 24 | 6 | 2 | 8
...
in "Diagnose Master"
iddm | code | name
1 | A.1 | ABC
2 | A.2 | ABCD
3 | B.3 | ABCDE
4 | B.4 | ABCDEF
5 | C.5 | ABCDEFG
...
I need to select diagnose table with idf1, idf2, idf3 field replace by field name from diagnose master so the output will be
idd | idmed | idf1 | idf2 | idf3
1 | 20 | ABCDEFG | ABCDEFGH | ABCDEFGHIJ
2 | 21 | ABCDE | - | ABC
...
How to query that?
Thank You
You need to join DiagnoseMaster three times to the same table on three different columns, using three different aliases. Try something like this:
SELECT d.idd, d.idmed, dm1.name idf1, dm2.name idf2, dm3.name idf3
FROM Diagnose d
JOIN DiagnoseMaster dm1 on dm1.idd = d.idf1
JOIN DiagnoseMaster dm2 on dm2.idd = d.idf2
JOIN DiagnoseMaster dm3 on dm3.idd = d.idf3

MySQL GROUP By Only when Two column matches

I am trying to group a record only if two of the fields repeat themselves.
I am designing a social sharing photo app. users can share, like and comment on thers photo. Each action (share, comment, like) will appear on their friends wall.
The Problem is that when a user do all the three actions, the picture appears three times instead of one with the three action on it.
Data in database is like this (activities_tb)
id | photoID | uiID | action | date
-------------------------------------------
1 | 1 | 2 | like | 01/01/2015
2 | 1 | 2 | share | 02/01/2015
3 | 1 | 4 | share | 03/01/2015
4 | 1 | 2 | comment | 04/01/2015
5 | 2 | 4 | like | 04/01/2015
6 | 2 | 2 | like | 05/01/2015
7 | 2 | 3 | share | 05/01/2015
8 | 2 | 4 | comment | 06/01/2015
8 | 3 | 3 | like | 07/01/2015
9 | 3 | 5 | like | 08/01/2015
10 | 3 | 5 | comment | 08/01/2015
The query result I want to get
id | photoID | uiID | action | date
-------------------------------------------
3 | 1 | 4 | share | 03/01/2015
4 | 1 | 2 | comment | 04/01/2015
6 | 2 | 2 | like | 05/01/2015
7 | 2 | 3 | share | 05/01/2015
8 | 2 | 4 | comment | 06/01/2015
8 | 3 | 3 | like | 07/01/2015
10 | 3 | 5 | comment | 08/01/2015
This is my statement
SELECT id, photoID, uiID, action, date
FROM activities_tb
GROUP BY photoID, uiID.
This combines all the photos by their id returning only three results
I will be glad if anyone can be of help, thank you
You can first select required ids and join on your table:
select tb.*
from activities_tb tb
join(select max(id) as id
from activities_tb
group by photoID, uiID) t on t.id = tb.id
You are looking for "SELECT DISTINCT"
SELECT DISTINCT photoID, uiID, action, date
FROM activities_tb
GROUP BY photoID, uiID.

How should I write this MySQL query containing multiple Left Joins

I have a query consisting of multiple joins and I am wondering whether it can be re-written to improve performance.
I have 2 tables as follows (I have removed non-important columns for this example):
slots
------------------------------------------
| id | name | slot_1 | slot_2 | slot_3 |
------------------------------------------
| 1 | Bob | 1 | 2 | 3 |
| 2 | Jim | 4 | 3 | 3 |
| 3 | Alf | 1 | 2 | 5 |
------------------------------------------
(There are 25 slots in total, each in it's own column)
slot_details
-----------------------------------
| id | stat_1 | stat_2 | stat_3 |
-----------------------------------
| 1 | 1 | 5 | 6 |
| 2 | 4 | 31 | 23 |
| 3 | 6 | 5 | 7 |
| 4 | 7 | 4 | 9 |
| 5 | 2 | 3 | 5 |
-----------------------------------
(There are 10 stats in total)
The query is as follows:
SELECT
slots.name,
slot_1_details.stat_1 AS slot_1_stat_1,
slot_1_details.stat_2 AS slot_1_stat_2,
slot_1_details.stat_3 AS slot_1_stat_3,
slot_2_details.stat_1 AS slot_2_stat_1,
slot_2_details.stat_2 AS slot_2_stat_2,
slot_2_details.stat_3 AS slot_2_stat_3,
slot_3_details.stat_1 AS slot_3_stat_1,
slot_3_details.stat_2 AS slot_3_stat_2,
slot_3_details.stat_3 AS slot_3_stat_3
FROM
slots
LEFT JOIN
slot_details AS slot_1_details
ON (
slot_1_details.id = slots.slot_1
)
LEFT JOIN
slot_details AS slot_2_details
ON (
slot_2_details.id = slots.slot_2
)
LEFT JOIN
slot_details AS slot_3_details
ON (
slot_3_details.id = slots.slot_3
)
WHERE (
slots.id = 1
)
The expected outcome of this query would be as follows:
| name | slot_1_stat_1 | slot_1_stat_2 | slot_1_stat_3 | slot_2_stat_1 | slot_2_stat_2 | slot_2_stat_3 | slot_3_stat_1 | slot_3_stat_2 | slot_3_stat_3 |
|bob | 1 | 5 | 6 | 4 | 31 | 23 | 6 | 5 | 7 |
Unfortunately I am not in a situation where I can change the tables.
Thank you for the help!
maybe
SELECT * FROM slots s LEFT JOIN slot_details sd ON s.id=sd.id
but i'm not sure because the query you posted is very confusing.
what are the keys of those tables?