query using inner joins and having - mysql

These are my tables:
mysql> select * from professor;
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
6 rows in set (0.00 sec)
mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
| 4 | 2 | 6 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
| 7 | 5 | 6 |
| 3 | 5 | 2 |
| 6 | 4 | 10 |
| 2 | 7 | 1 |
+----------+-------+---------+
15 rows in set (0.00 sec)
mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 5 |
| 2 | Science | 1 |
| 3 | English | 6 |
| 4 | Social | 4 |
| 5 | Malayalam | 20 |
| 6 | Arts | 25 |
| 7 | Biology | 20 |
+----------+------------+--------+
7 rows in set (0.00 sec)
The question is :
Return those courses that have been taught by all professors.
The query I tries is:
select course.coursename from
course inner join works
on course.courseid=works.empid
group by works.courseid
having works.empid in (select empid from professor);
I am getting an error like this:
Unknown column 'works.empid' in 'IN/ALL/ANY subquery'
Pls help me out with the query.
http://sqlfiddle.com/#!2/4b197/5

Apart from the reasons why your current query gives you an error, which #GordonLinoff explained in detail, one way to achieve the desired result
Return those courses that have been taught by all professors.
is
SELECT c.*
FROM
(
SELECT courseid
FROM works
GROUP BY courseid
HAVING COUNT(DISTINCT empid) =
(
SELECT COUNT(*)
FROM professor
)
) q JOIN course c
ON q.courseid = c.courseid
Note: Thanks to #eggyal it's worth to mention that this query operates on the assumption that referential integrity is intact meaning works table doesn't have orphaned records (rows where empid refers to a non-existent row in professor table) and technically returns courses taught by the same number of professors as currently exist in the professor table which in the case of intact referential integrity happen to be the courses we're looking for.
Output:
| COURSEID | COURSENAME | POINTS |
|----------|------------|--------|
| 2 | Science | 1 |
Here is SQLFiddle demo

First, the query is way off from what you want. I'm only going to address the error.
The error is very interesting. In short, MySQL allows something called hidden columns (which are described below). However, these only work in the having clause when they are included in the select clause. I hadn't known that.
The following two queries parse correctly (I'm skipping the middle part for brevity):
select course.coursename, works.empid
. . .
having works.empid = 1;
select course.coursename, works.empid
. . .
having works.empid = 1;
And yet, the following two fail with the same error:
select course.coursename
. . .
having works.empid = 1;
select course.coursename
. . .
having works.empid = 1;
The only difference s that the column is not mentioned in the select clause.
What is happening is that you are using a MySQL extension to the group by clause, sometimes called "Hidden Columns". You have columns in the select or having clause that are neither aggregation keys (course.courseid) nor surrounded by an aggregation function (say min(works.empid) or group_concat(works.empid). Apparently, MySQL only recognizes these columns in the having clause when they are already in the `select clause. At your stage of learning SQL, you just shouldn't do this. Following the documentation to turn off this extension and go to ANSI standard behavior:
To disable the MySQL GROUP BY extension, enable the ONLY_FULL_GROUP_BY
SQL mode. This enables standard SQL behavior: Columns not named in the
GROUP BY clause cannot be used in the select list or HAVING clause
unless enclosed in an aggregate function.
The way to fix the syntax problem is to use an aggregation function, something like:
select course.coursename
. . .
having min(works.empid) = 1;
select course.coursename
. . .
having min(works.empid) = 1;
This will get you no closer to having a working query, because yours is far from solving the problem. But it will fix the syntactic error.

Make a cross-join between course and professor to obtain every combination of courses and professors;
Make an outer join between that and works to identify which of those (course, professor) combinations actually exist;
Group by course and sum the number of such combinations that do not exist;
Filter the groups for only those where there are no such non-existent combinations.
Therefore:
SELECT course.*
FROM (course, professor) LEFT JOIN works USING (courseid, empid)
GROUP BY courseid
HAVING SUM(works.empid IS NULL) = 0

try this:
select c.coursename, w.empid from
course as c inner join works as w
on (c.courseid=w.empid)
group by w.courseid
having w.empid in (select p.empid from professor as p);

select course.coursename,works.courseid,works.empid from
course inner join works
on course.courseid=works.empid
group by works.courseid
having works.empid in (select empid from professor);
Its Working

Related

Combining three SQL queries into one

I got working code from three queries but I would like to combine them into one or two. Basically I am checking if a provided phone number exists in table contacts or leads as well as if it exists as a secondary number in customfieldsvalues (not all leads have a customfield value though). I am using a CRM system based on CodeIgniter.
What I want to do (non-correct/hypothetical query):
SELECT * FROM contacts OR leads WHERE phonenumber = replace(X, '-', '')
OR leads.id = customvaluefields.relid AND cfields.fieldid = 41 AND cfields.value = X
Tables
table : contacts
+-------+----------------+----------------+
| id | firstname | phonenumber |
+-------+----------------+----------------+
| 1 | John | 214-444-1234 |
| 2 | Mary | 555-111-1234 |
+-------+----------------+----------------+
table : leads
+-------+-----------+---------------------+
| id | name | phonenumber |
+-------+-----------+---------------------+
| 1 | John | 214-444-1234 |
| 2 | Mary | 555-111-1234 |
+-------+-----------+---------------------+
table : customvaluefields
+-------+-----------+-------------+-----------+
| id | relid | fieldid | value |
+-------+-----------+-------------+-----------+
| 1 | 1 | 41 | 222333444 |
| 2 | 1 | 20 | Management|
| 3 | 2 | 41 | 333444555 |
+-------+-----------+-------------+-----------+
If I understand what you are trying to, maybe UNION ALL would work. This is something to get you started:
SELECT C.ID, C.FirstName, C.Phonenumber
FROM Contacts C
JOIN CustomValueField CVF
ON c.ID = CVF.RelID AND
CVF.ID = 41
AND REPLACE(Phonenumber,'-','') = cvf.Value
UNION ALL
SELECT L.ID, L.FirstName, L.Phonenumber
FROM Leads L
JOIN CustomValueField CVF
ON L.ID = CVF.RelID AND
CVF.ID = 41
AND REPLACE(Phonenumber,'-','') = cvf.Value
I'm joining the contacts and leads tables to CustomeValueField in each query and then UNION them together along with the WHERE clause in each. I'm sure it's not 100% correct for what you need, but should get you headed to a solution. Here is more information: https://dev.mysql.com/doc/refman/8.0/en/union.html

Sum of Counted records that calculated using "group by" with condition and "group by"

I'm sorry for fuzzy title of this question.
I have 2 Tables in my database and want to count records of first_table using "group by" on a foreign key id that exists in a column of second_table (which stores ids like array "1,2,3,4,5").
id | name | fk_id
1 | john | 1
2 | mike | 1
3 | jane | 2
4 | tailor | 1
5 | jane | 3
6 | tailor | 5
7 | jane | 4
8 | tailor | 5
9 | jane | 5
10 | tailor | 5
id | name | fk_ids | s_fk_id
1 | xxx | 1,5,6 | 1
2 | yyy | 2,3 | 1
3 | zzz | 9 | 1
4 | www | 7,8 | 1
Now i wrote the following query but it not working properly and displays wrong numbers.
I WANT TO:
1-Count records in first_table group by "fk_id"
2-Sum the counted records which exists in "fk_ids"
3-Display the sum result (sum of related counts) grouped by id.
symbol ' ' means ``.
select sum(if(FIND_IN_SET('fk_id', 'fk_ids')>0,'count',0) 'sum', 'count', 'from'.'fk_id', 'second_table'.* FROM 'second_table'
LEFT JOIN
(
SELECT 'fk_id', count(*) 'count'
FROM 'first_table'
group BY 'fk_id'
) AS 'from'
ON FIND_IN_SET('fk_id', 'fk_ids')>0
WHERE 'second_table'.'s_fk_id'=1
GROUP BY 'id'
ORDER by 'count' DESC
This table has many data and we have no plan to change the structure.
Edit:
Desired output:
id | name | sum
1 | xxx | 7 (3+4+0)
2 | yyy | 2 (1+1)
3 | zzz | 0 (0)
4 | www | 0 (0+0)
After two holidays i came back to work and found out that the "FIND_IN_SET" function is not working properly with space contained string.
And the problem is that i was ignored the spaces too, (same as this question)
Finnaly this query worked:
select sum(`count`) `sum`, `count`, `from`.`fk_id`, `second_table`.* FROM `second_table`
LEFT JOIN
(
SELECT `fk_id`, count(*) `count`
FROM `first_table`
group BY `fk_id`
) AS `from`
ON FIND_IN_SET(`fk_id`, replace(`fk_ids`,' ',''))>0
WHERE `second_table`.`s_fk_id`=1
GROUP BY `id`
ORDER by `count` DESC
And the magic is replace(fk_ids,' ','')

MySQL Concat/Trim in Select query to check if exists in another table - What's wrong with my code?

Table_Base
+----+----------------+
| ID | ACCOUNT |
+----+----------------+
| 1 | 100 |
| 2 | 120 |
| 3 | 193 |
| 4 | 201 |
| 5 | 213 |
| 6 | 247 |
| 7 | 304 |
+----+----------------+
Table_Transform
+----+----------------+
| ID | Account_Number |
+----+----------------+
| 1 | 100 |
| 2 | 9120 |
| 3 | 193 |
| 4 | 9201 |
| 5 | 9213 |
| 6 | 442 |
| 7 | 589 |
+----+----------------+
All the entries in the ACCOUNT column have multiple spaces in front of them, so I use the TRIM() function.
I need to return all ACCOUNTS in Table_Base that DO NOT appear in Account_Number in Table_Transform, while taking into account that some ACCOUNTS appear in Table_Transform with a 9 in front of them. Therefore, the ACCOUNTS that should be returned are
247
304
However, with my code, it is ignoring the AND clause and returning those that appear in Table_Transform that have the 9 in front of them. What should I fix?
SELECT * FROM Table_Base
WHERE ACCOUNT NOT IN
(SELECT Account_Number FROM Table_Transform)
AND CONCAT(9,TRIM(ACCOUNT)) NOT IN (SELECT Account_Number FROM Table_Transform);
I believe I have a problem with the CONCAT query, as I cannot even get it to work by itself in a one line SELECT statement (it will still return incorrectly).
Try this
SELECT *
FROM Table_Base
WHERE TRIM(ACCOUNT) NOT IN
(SELECT Account_Number FROM Table_Transform)
AND CAST(CONCAT('9',TRIM(ACCOUNT)) AS UNSIGNED) NOT IN (SELECT Account_Number FROM Table_Transform);
--EDIT added CAST to make extra sure --
for a direct equivalency. I would use a JOIN statement, if you're interested
I would suggest NOT EXISTS. One method is:
SELECT b.*
FROM Table_Base b
WHERE NOT EXISTS (SELECT 1
FROM Table_Transform t
WHERE t.Account_Number = trim(b.account) OR
t.Account_Number = concat(9, trim(b.account)
);
For performance reasons, though, I would split this into two expressions:
SELECT b.*
FROM Table_Base b
WHERE NOT EXISTS (SELECT 1
FROM Table_Transform t
WHERE t.Account_Number = trim(b.account)
) AND
NOT EXISTS (SELECT 1
FROM Table_Transform t
WHERE t.Account_Number = concat(9, trim(b.account))
);
This can take advantage of an index on Table_Transform(Account_Number).

How to join tables with SQL query and take number of tied columns?

I'm having BookTable in database (with foregin hey LibID):
| BookID | BookName | BookPrice | LibID |
-------------------------------------------
| 1 | Book_1 | 200 | 1 |
| 2 | Book_2 | 100 | 1 |
| 3 | Book_3 | 300 | 2 |
| 4 | Book_4 | 150 | 4 |
and also LibraryTable:
| LibID | LibName | LibLocation |
-----------------------------------
| 1 | Lib_1 | Loc_1 |
| 2 | Lib_2 | Loc_2 |
| 3 | Lib_3 | Loc_3 |
| 4 | Lib_4 | Loc_4 |
I need to write SQL query that will return be the info about the library and number of books for that library:
| LibID | LibName | NumberOfBooks|
------------------------------------
| 1 | Lib_1 | 2 |
| 2 | Lib_2 | 1 |
| 3 | Lib_3 | 0 |
| 4 | Lib_4 | 1 |
It should be one SQL query, probably with nested queries or joins.. Not sure how the query should look like:
SELECT L.LibID AS LibID, L.LibName AS LibName, COUNT(B) AS NumberOfBooks
FROM LibraryTable L, BookTable B
WHERE L.LibID = B.LibID
Will that work?
No, this query will not work. COUNT aggregates data, so you must explicitely tell the DBMS for which group of data you want the count. In your case this is the library (you want one result record per library).
COUNT's parameter is a column, not a table, so change this to * (i.e. count records) or a certain column (e.g. LibID).
The join syntax you are using is valid, but deprecated. Use explicit joins instead. In your case an outer join would even show libraries that have no books at all, if such is possible.
select l.libid, l.libname, count(b.libid) as numberofbooks
from librarytable l
left outer join booktable b on b.libid = l.libid
group by l.libid;
You could also do all this without a join at all and get the book count in a subquery instead. Then you wouldn't have to aggregate. That's way simpler and more readable in my opinion.
select
l.libid,
l.libname,
(select count(*) booktable b where b.libid = l.libid) as numberofbooks
from librarytable l;
SELECT lt.LibID AS LibID, lt.LibName AS LibName, count(*) AS NumberOfBooks
FROM BookTable AS bt
LEFT JOIN LibraryTable AS lt ON bt.LibID = lt.LibID
GROUP BY bt.LibID

LIMIT results to n unique column values?

I have some MySQL results like this:
---------------------------
| name | something_random |
---------------------------
| john | ekjalsdjalfjkldd |
| alex | akjsldfjaekallee |
| alex | jkjlkjslakjfjflj |
| alex | kajslejajejjaddd |
| bob | ekakdie33kkd93ld |
| bob | 33kd993kakakl3ll |
| paul | 3k309dki595k3lkd |
| paul | 3k399kkfkg93lk3l |
etc...
This goes on for 1000's of rows of results. I need to limit the number of results to the first 50 unique names. I think there is a simple solution to this but I'm not sure.
I've tried using derived tables and variables but can't quite get there. If I could figure out how to increment a variable once every time a name is different I think I could say WHERE variable <= 50.
UPDATED
I've tried the Inner Join approach(es) suggested below. The problem is this:
The subselect SELECT DISTINCT name FROM testTable LIMIT 50 grabs the first 50 distinct names. Perhaps I wasn't clear enough in my original post, but this limits my query too much. In my query, not every name in the table is returned in the result. Let me modify my original example:
----------------------------------
| id | name | something_random |
----------------------------------
| 1 | john | ekjalsdjalfjkldd |
| 4 | alex | akjsldfjaekallee |
| 4 | alex | jkjlkjslakjfjflj |
| 4 | alex | kajslejajejjaddd |
| 6 | bob | ekakdie33kkd93ld |
| 6 | bob | 33kd993kakakl3ll |
| 12 | paul | 3k309dki595k3lkd |
| 12 | paul | 3k399kkfkg93lk3l |
etc...
So I added in some id numbers here. These ID numbers pertain to the people's names in the tables. So you can see in the results, not every single person/name in the table is necessarily in the result (due to some WHERE condition). So the 50th distinct name in the list will always have an ID number higher than 49. The 50th person could be id 79, 234, 4954 etc...
So back to the problem. The subselect SELECT DISTINCT name FROM testTable LIMIT 50 selects the first 50 names in the table. That means that my search results will be limited to names that have ID <=50, which is too constricting. If there are certain names that don't show up in the query (due to some WHERE condition), then they are still counted as one of the 50 distinct names. So you end up with too few results.
UPDATE 2
To #trapper: This is a basic simplification of what my query looks like:
SELECT
t1.id,
t1.name,
t2.details
FROM t1
LEFT JOIN t2 ON t1.id = t2.some_id
INNER JOIN
(SELECT DISTINCT name FROM t1 ORDER BY id LIMIT 0,50) s ON s.name = t1.name
WHERE
SOME CONDITIONS
ORDER BY
t1.id,
t1.name
And my results look like this:
----------------------------------
| id | name | details |
----------------------------------
| 1 | john | ekjalsdjalfjkldd |
| 3 | alex | akjsldfjaekallee |
| 3 | alex | jkjlkjslakjfjflj |
| 4 | alex | kajslejajejjaddd |
| 6 | bob | ekakdie33kkd93ld |
| 6 | bob | 33kd993kakakl3ll |
| 12 | paul | 3k309dki595k3lkd |
| 12 | paul | 3k399kkfkg93lk3l |
...
| 37 | bill | kajslejajejjaddd |
| 37 | bill | ekakdie33kkd93ld |
| 41 | matt | 33kd993kakakl3ll |
| 50 | jake | 3k309dki595k3lkd |
| 50 | jake | 3k399kkfkg93lk3l |
----------------------------------
The results stop at id=50. There are NOT 50 distinct names in the list. There are only roughly 23 distinct names.
My MySql syntax may be rusty, but the idea is to use a query to select the top 50 distinct names, then do a self-join on name and select the name and other information from the join.
select a.name, b.something_random
from Table b
inner join (select distinct name from Table order by RAND() limit 0,50) a
on a.name = b.name
SELECT DISTINCT name FROM table LIMIT 0,50
Edited: Ahh yes I misread question first time, this should do the trick though :)
SELECT a.name, b.something_random
FROM `table` b
INNER JOIN (SELECT DISTINCT name FROM `table` ORDER BY RAND() LIMIT 0,50) a
ON a.name = b.name ORDER BY a.name
How this work is the (SELECT DISTINCT name FROMtableORDER BY RAND() LIMIT 0,50) part is what pulls out the names to include in the join. So here I am taking 50 unique names at random, but you can change this to any other selection criteria if you want.
Then you join those results back into your table. This links each of those 50 selected names back to all of the rows with a matching name for your final results. Finally ORDER BY a.name just to be sure all the rows for each name end up grouped together.
This should do it:
SELECT tA.*
FROM
testTable tA
INNER JOIN
(SELECT distinct name FROM testTable LIMIT 50) tB ON tA.name = tB.name
;