solve mysql query - mysql

Today I have been asked a question by an interviewer that stated
we have three tables named as table A, B, and C.
Those tables are like this
A B C
------------------ -------------------------- ----------------------------
| ID | ProjectID | | ID | LocationID | aID | | ID | points | LocationID |
------------------ -------------------------- ----------------------------
| 1 | 15 | | 1 | 131 | 1 | | 1 | 123333 | 131 |
| 2 | 15 | | 2 | 132 | 1 | | 2 | 123223 | 132 |
| 3 | 15 | | 3 | 133 | 1 | | 3 | 522 | 211 |
| 4 | 12 | | 4 | 134 | 2 | | 4 | 25 | 136 |
------------------ | 5 | 136 | 2 | | 5 | 25 | 133 |
| 6 | 137 | 3 | | 6 | 25 | 134 |
| 7 | 138 | 1 | | 7 | 25 | 135 |
-------------------------- ----------------------------
now he told me to write a query that sums the points of those locations whose project is 15.
First i wrote the query to get ID's from table A like this
SELECT ID from A where projectID = 15
then i pass this result in table b query just like this
SELECT LocationID FROM B WHERE aID IN ( SELECT ID from A where projectID = 15 )
Then i calculate the sum of these locations just like this
SELECT SUM(points) from C where LocationID IN(SELECT LocationID FROM B WHERE aID IN ( SELECT ID from A where projectID = 15))
My Result is fine and query is correct. But he rejected my answer by saying that this nested IN Clause will slow down the whole process as when we have thousands of records.
Then he gave me another chance to review my answer but i couldn't figure it out.
Is there anyway to optimize this or is there some other way to do the same.
Any help? Thanks

Try this it may solve your problem.
Select SUM(C.points) FROM C JOIN B ON C.LocationID = B.LocationID JOIN A ON B.aID = A.ID where A.ProjectID = 15 GROUPBY A.ProjectID

Try with this....i hope it will work
select sum(c.points) as sum_points
from A a,B b,C c where
a.ID=b.aID and
b.LocationID=c.LocationID
and a.projectID=15

Related

Extracting data from table with results from different search

I am trying to extract data from tables with the results from a previous search. I am not really familiar with database query's and have made one that will crash my computer from drawing too much memory.
This data is coming from a board tester and I want certain information.
How many boards were ran during a given period
How many failed
All the failure data for those boards EDIT: This is the one I need to figure out. See Edit at bottom.
The first time a board is ran it creates a record in the Board table
+----------+-------+-----+
| Board_id | Board | rev |
+----------+-------+-----+
| 1 | 1234 | 1 |
| 2 | 1234 | 1 |
| 3 | 1235 | 2 |
| 4 | 5869 | 15 |
+----------+-------+-----+
Each time the board is ran it creates a Test record
+----------+----------+---------+---------------------+
| Test_id | Board_id | Operator| Date_Time |
+----------+----------+---------+---------------------+
| 34 | 1 | 1 | 2017-08-02 09:13:34 |
| 35 | 1 | 1 | 2017-08-02 09:13:36 |
| 36 | 1 | 1 | 2017-08-02 09:13:39 |
| 37 | 2 | 1 | 2017-08-02 09:14:10 |
| 38 | 3 | 1 | 2017-08-02 09:16:24 |
| 39 | 3 | 2 | 2017-08-03 10:40:45 |
| 40 | 4 | 2 | 2017-08-03 10:43:34 |
+----------+----------+---------+---------------------+
...and Results are stored in Results
+-----------+---------+--------+-------------+-------------+
| Result_id | Test_id | Result | Upper_Limit | Lower_Limit |
+-----------+---------+----------------------+-------------+
| 40 | 34 | 2 | 4 | 1 |
| 41 | 34 | 3 | 4 | 1 |
| 42 | 34 | 4 | 4 | 1 |
| 43 | 34 | 0 | 4 | 1 |
| 44 | 35 | 2 | 4 | 1 |
| 45 | 35 | 3 | 4 | 1 |
| 46 | 35 | 4 | 4 | 1 |
| 47 | 35 | 0 | 4 | 1 |
| 48 | 36 | 2 | 4 | 1 |
| 49 | 36 | 3 | 4 | 1 |
| 50 | 36 | 4 | 4 | 1 |
| 51 | 36 | 2 | 4 | 1 |
| 52 | 37 | 2 | 4 | 1 |
| 53 | 37 | 3 | 4 | 1 |
| 54 | 37 | 4 | 4 | 1 |
| 55 | 37 | 2 | 4 | 1 |
| 56 | 38 | 2 | 4 | 1 |
| 57 | 38 | 3 | 4 | 1 |
| 58 | 38 | 4 | 4 | 1 |
| 59 | 38 | 5 | 4 | 1 |
| 60 | 39 | 2 | 4 | 1 |
| 61 | 39 | 3 | 4 | 1 |
| 62 | 39 | 4 | 4 | 1 |
| 63 | 39 | 5 | 4 | 1 |
| 64 | 40 | 2 | 4 | 1 |
| 65 | 40 | 3 | 4 | 1 |
| 66 | 40 | 4 | 4 | 1 |
| 67 | 40 | 3 | 4 | 1 |
+-----------+---------+--------+-------------+-------------+
To get the number of boards, and Board_ID, ran during a given period I query.
SELECT a.Board_ID FROM
Tests a, Results b
WHERE a.Date_Time>='2017-08-02' AND a.Date_Time<'2017-08-03' and
a.Test_ID = b.Test_ID
group by a.Board_ID
To get all associated test to those Board_ID's I query.
SELECT * from
Tests x, (
SELECT a.Board_ID FROM
Tests a, Results b
WHERE a.Date_Time>='2017-08-02' AND a.Date_Time<'2017-08-03' and
a.Test_ID = b.Test_ID
group by a.Board_ID
) y
where x.Board_ID = y.Board_ID
This gives me the correct results, but the query seems off, but when I try to get the failed results from the query above is when I have the most trouble.
SELECT d.Test_ID FROM
Boards a, Tests b, (
SELECT x.Test_ID, x.Board_ID, x.Operator, x.Date_Time from
Tests x, (
SELECT a.Board_ID FROM
Tests a, Results b
WHERE a.Date_Time>='2017-08-02' AND a.Date_Time<'2017-08-03' and
a.Test_ID = b.Test_ID
group by a.Board_ID
) y
)d
WHERE d.Test_ID = b.Test_ID and
b.Result not between Lower_Limit and Upper_Limit
EDIT:
If you look at the Test table I created you will see that board_id 3 got tested twice and on two different days. I need to see the boards that we ran on a given day, this example 2017-08-02, and all associated records to those boards. So since Board_ID #3 was ran on 2 days, and was ran on the day in question, I would need that record included in my query.
My Solution
SELECT * FROM
(
SELECT x.Test_ID, x.Board_ID, x.Operator, x.Date_Time from
Test x, (
SELECT a.Board_ID FROM
Test a
join Results b on a.Test_ID = b.Test_ID
WHERE a.Date_Time>='2017-08-11' AND a.Date_Time<'2017-08-12'
group by a.Board_ID
) y
where x.Board_ID = y.Board_ID
)d
join Boards a on a.Board_ID = d.Board_ID
join Results b on b.Test_ID = d.Test_ID
join Test_Names c on c.Test_Name_ID = b.Test_Name_ID --Table Not shown
WHERE
b.result not between Lower_Limit and Upper_Limit
From this you see I have 3 nested searches into 1. With the 3 individual searches I get all the data I need to parse the information I want. Next will be to find a way to query the database for what I need instead of parsing.
I think you're overthinking this. You don't need all the inline views. Here's how I would write it using ANSI Joins (like #CptMisery suggested in the comments)
SELECT d.test_id, b.board, b.board_rev, r.result_id, r.result -- and whatever else you need.
from tests t
join results r on t.test_id = r.test_id
join boards b on t.board_id = b.board_id
where t.Date_Time>='2017-08-02' AND t.Date_Time<'2017-08-03'
and r.result >Lower_Limit -- or >=
and r.result < Upper_Limit -- or <=, if it can be the limit value
JOIN all the tables based on their relationships (Foreign Key to Primary Key), choose your filters in the where clause, and choose the columns to "project" with Select.
SELECT d.Test_ID FROM
Boards a, Tests b, ( SELECT x.Test_ID,
x.Board_ID,
x.Operator,
x.Date_Time
from Tests x,
(SELECT a.Board_ID
FROM Tests a, Results b
WHERE a.Date_Time>='2017-08-02'
AND a.Date_Time<'2017-08-03'
and a.Test_ID = b.Test_ID
group by a.Board_ID
) y
)d
WHERE d.Test_ID = d.Test_ID
and b.Result >= Lower_Limit
and b. Result <=Upper_Limit

MySQL Query to get Similar likes

I am designing a simple architecture where i have a table which stores users and some elements that they like so my table structure is something like this:
+---------+---------+
| user_id | like_id |
+---------+---------+
| 1 | 4 |
| 2 | 2 |
| 4 | 4 |
| 4 | 3 |
| 5 | 4 |
| 6 | 7 |
| 7 | 5 |
| 34 | 6 |
| 3 | 8 |
| 2 | 3 |
| 2 | 5 |
| 1 | 3 |
| 1 | 10 |
| 1 | 12 |
| 2 | 10 |
+---------+---------+
Now what i will have is id of any user (lets say user_id = 1 ) and i want a query to get all the other users who have similar Likes as that of 1.
So in the Output for user_id = 1 will be :
+---------------------------+------------------------+----------------+
| users_with_common_likes | no_of_common_likes | common_likes |
+---------------------------+------------------------+----------------+
| 4 | 2 | 3,4 |
| 2 | 2 | 3,10 |
| 5 | 1 | 4 |
+---------------------------+------------------------+----------------+
What I have achieved :
I can do this using a sub-query as below :
SELECT user_id
FROM `user_likes`
WHERE `like_id`
IN (
SELECT GROUP_CONCAT( `like_id` )
FROM user_likes
WHERE user_id =1
)
AND user_id !=1
LIMIT 0 , 30
However this query is not giving all the users,it misses the user_id = 2 which has like id 3 in common with user_id=1.
and i cant figure out how to find the remaining 2 columns.
Also I feel that this is not the best way to to this as this table will contain thousands of data and it may effect system performance.
I would like to do this with a single Mysql Query.
This assumes a PK formed on user_id,like_id...
SELECT y.user_id
, GROUP_CONCAT(y.like_id) likes
, COUNT(*) total
FROM my_table x
JOIN my_table y
ON y.like_id = x.like_id
AND y.user_id <> x.user_id
WHERE x.user_id = 1
GROUP
BY y.user_id;

Select the name and value of a colmun using 2 different inner joined columns' values

I'm not sure if MySQL is capable of doing something like this. I'm no MySQL master by any means but to me this seems to be like it might be an example of a Pivot Table?
I've looked at other Pivot Table example and I find them confusing at best and I'm still not quite sure if it's relevant in my case.
Here's an example of the data and tables in question:
Foo Table:
RowID | col_one | col_two | col_three
------|---------|---------|-----------
1 | 2 | 34 | 64
2 | 6 | 53 | 23
3 | 8 | 22 | 45
Foo_Meta Table:
RowID | FooID | MetaName | MetaValue
------|-------|----------|-----------
1 | 1 | This | 302
2 | 1 | That | 466
3 | 1 | Other | 132
4 | 2 | This | 222
5 | 2 | That | 87
6 | 2 | Other | 400
7 | 3 | This | 732
8 | 3 | That | 55
9 | 3 | Other | 690
Here's an example of the select I'm using but isn't quite what I'm looking for:
Select:
SELECT
t.col_one,
t.col_two,
t.col_three,
m.MetaName,
m.MetaValue
FROM
foo t
INNER JOIN
foo_meta m ON t.RowID = m.FooID
Here's an example of the table I'm trying to Select:
Select:
RowID | col_one | col_two | col_three | This | That | Other
------|---------|---------|---------------------------------
1 | 2 | 34 | 64 | 302 | 466 | 132
2 | 6 | 53 | 23 | 222 | 87 | 400
3 | 8 | 22 | 45 | 732 | 55 | 690
Yes, you are pivoting foo_meta fields - at least some versions of Oracle do that, I'm quite certain MySQL doesn't.
Generally, to "fake" a pivot in MySQL, you do multiple joins. Something like:
SELECT
t.col_one,
t.col_two,
t.col_three,
mthis.this,
mthat.that,
mother.other
FROM
foo t
INNER JOIN
(select fooid, MetaValue as This from foo_meta where MetaName = 'This') mthis ON t.RowID = mthis.FooID
INNER JOIN
(select fooid, MetaValue as That from foo_meta where MetaName = 'That') mthat ON t.RowID = mthat.FooID
INNER JOIN
(select fooid, MetaValue as Other from foo_meta where MetaName = 'Other') mother ON t.RowID = mother.FooID
You might have to use left outers depending on how your data is populated.

Big Mysql Query (Join counts in the result and modify a string)

I am brand new to mysql so please excuse my level of knowledge here, and feel free to direct me in a better direction if what I am doing is out of date.
I am pulling information from a database to fill in a php page.
My tables:
Server:
|ServerID (int) | ArticleID (int) | locationID (int) |
| 1 | 46 | 55 |
| 2 | 11 | 81 |
| 3 | 81 | 46 |
| 4 | 55 | 11 |
| 5 | 81 | 99 |
| 5 | 11 | 52 |
Article:
|ArticleID (int) | Name (varchar) | Typ (int) |
| 46 | domain | 0 |
| 81 | root-server | 1 |
| 55 | vserver | 2 |
| 11 | root-server2 | 1 |
Location:
|LocationID (int) | location (varchar) |
| 46 | 1-5-15-2 |
| 81 | 1-5-14-2 |
| 55 | 2-25-1-9 |
| 11 | 21-2-5-8 |
| 99 | 17-2-5-8 |
| 52 | 1-8-5-8 |
Result:
|location (int) | name (varchar) | count (int) |
| 1 | root-server | 1 |
| 1 | root-server2 | 2 |
| 17 | root-server | 1 |
The location in the result is the first number block of the location in the location table (1-5-15-2 -> 1, 1-8-5-8 -> 1, 21-2-5-8 -> 21, 17-2-5-8 -> 17).
The count is the sum of all servers with the same name and the same first location block.
Do anyone think its possible to get this result in only one query?
Thanks for any answer!
Check this
SELECT SUBSTRING_INDEX(location, '-', 1) as LID,Article.Name,Count(*) as Count
from Location join Server
on Server.locationID=Location.locationID
join Article on Article.ArticleID=Server.ArticleID
group by LID,Article.ArticleID ;
DEMO
Please give this a shot:
SELECT
s.locationID as id, a.name, count(*) as count
FROM
`Server` s
LEFT JOIN
`Article` a ON s.ArticleID = a.ArticleID
GROUP BY s.locationID, a.name
something like this should work
select
s.location_id as location, a.name, count(location) as count
from
server as s, article as a
where
s.articleID = a.articleID
group by location, a.name

Joining 3 tables

First off, sorry if this is a near enough duplicate. I've found this question, which nearly does what I want, but I couldn't wrap my head around how to alter it to my needs.
I've got these 3 tables:
cs_Accounts:
+----+-----------------------------+-------------+
| id | email | username |
+----+-----------------------------+-------------+
| 63 | jamasawaffles#googlil.com | jamwaffles2 |
| 64 | jamwghghhfles#goomail.com | jamwaffles3 |
| 65 | dhenddfggdfgetal-pipdfg.com | dhendu9411 |
| 60 | jwapldfgddfgfffles.co.uk | jamwaffles |
+----+-----------------------------+-------------+
cs_Groups:
+----+-----------+------------+-------------+
| id | low_limit | high_limit | name |
+----+-----------+------------+-------------+
| 1 | 0 | 0 | admin |
| 2 | 1 | 50 | developer |
| 3 | 76 | 100 | reviewer |
| 4 | 51 | 75 | beta tester |
| 5 | 1 | 50 | contributor |
+----+-----------+------------+-------------+
cs_Permissions:
+----+---------+----------+
| id | user_id | group_id |
+----+---------+----------+
| 4 | 60 | 4 |
| 3 | 60 | 1 |
| 5 | 60 | 2 |
| 6 | 62 | 1 |
| 7 | 62 | 3 |
+----+---------+----------+
I've been wrestling with a 3 way join for hours now, and I can't get the results I want. I'm looking for this behaviour: a row will be returned for every user from cs_Accounts where there is a row in cs_Permissions that contains their ID and the ID of a group from cs_Groups, as well as the group with the group_id has a high_lmiit and low_limit in a range I can specify.
Using the data in the tables above, we might end up with something like this:
email username cs_Groups.name
----------------------------------------------------------
jwapldfgddfgfffles.co.uk jamwaffles admin
jwapldfgddfgfffles.co.uk jamwaffles developer
jwapldfgddfgfffles.co.uk jamwaffles beta tester
dhenddfggdfgetal-pipdfg.com dhendu9411 admin
dhenddfggdfgetal-pipdfg.com dhendu9411 reviewer
There is an extra condition, however. This condition is where rows are only selected if the group the user belongs to has a high_limit and low_limit with values I can specify using a WHERE clause. As you can see, the table above only contains users with rows in the permissions table.
This feels a lot like homework but with a name like James I'm always willing to help.
select a.email,a.username,g.name
from cs_Accounts a
inner join cs_Permissions p on p.user_id = a.id
inner join cs_Groups g on g.id = p.Group_id
where g.low_limit > 70
and g.high_limt < 120
This is the query
SELECT ac.email, ac.username, gr.name
FROM cs_Accounts AS ac
LEFT JOIN cs_Permissions AS per ON per.user_id = ac.id
INNER JOIN cs_Groups AS gr ON per.user_id = gr.id
You can add a WHERE clause to this query if you want