SQL count rows of one table in relation to another table - mysql

I have 3 different tables :
Client
+----+-----------+----------+
| id | firstName | lastName |
+----+-----------+----------+
| 1 | John | Doe |
| 2 | Jane | Doe |
+----+-----------+----------+
Loan
+----+--------+-----------+----------------+
| id | amount | client_id | institution_id |
+----+--------+-----------+----------------+
| 1 | 200 | 2 | 3 |
| 2 | 400 | 1 | 1 |
+----+--------+-----------+----------------+
Institution
+----+---------------+
| id | name |
+----+---------------+
| 1 | Institution A |
| 2 | Institution B |
| 3 | Institution C |
+----+---------------+
I am looking to create a list of the number of loans a client has with each institution (for every row in the institution table). Including when a client has 0 loans with an institution.
Something that looks like :
+-----------+-----------+----------+--------------------------+-----------+
| client_id | firstName | lastName | financialInstitutionName | loanCount |
+-----------+-----------+----------+--------------------------+-----------+
| 1 | John | Doe | Institution A | 1 |
| 1 | John | Doe | Institution B | 0 |
| 1 | John | Doe | Institution C | 0 |
| 2 | Jane | Doe | Institution A | 0 |
| 2 | Jane | Doe | Institution B | 0 |
| 2 | Jane | Doe | Institution C | 1 |
+-----------+-----------+----------+--------------------------+-----------+
I have tried all manners of joins, subqueries and where clauses but without success. The concept that I do not grasp is how to get a row per institution, per client (total count institution x client). I would love if that query was possible without subqueries or union joins.
Thank you for your time!

First subquery in the FROM setups that data so each client has a record for each of the institutions. This is then joined to a subquery that counts the number of loans.
SELECT
d.client_id,
d.firstName,
d.lastName,
d.name AS financialInstitutionName,
CASE WHEN l IS NULL
THEN 0
ELSE l.loanCount
END AS loanCount
FROM
(
SELECT
Client.id AS client_id,
Client.firstName,
Client.lastName,
Institution.id AS institution_id,
Institution.name
FROM Client, Institution
) AS d
LEFT JOIN (
SELECT client_id, institution_id, COUNT(id) AS loanCount
FROM Loan
GROUP BY client_id, institution_id
) AS l ON d.client_id = l.client_id AND d.institution_id = l.institution_id
Edit: Includes a record for each institution
Edit: Spelling

SELECT
loan.client_id,
client.firstName,
client.lastName,
institution.name as financialInstitutionName,
COUNT(loan.id) as loanCount
FROM client
INNER JOIN loan ON client.id = loan.client_id
INNER JOIN institution ON loan.institution_id = institution.id
GROUP BY client.id;

Related

SQL search merge

I'm trying to merge two select sentence. Is it possible ?
I have three tables :
score
+------+---------+-------+
| ID | SUBJECT | SCORE |
+------+---------+-------+
| 1 | Chinese | 65 |
| 1 | English | 75 |
| 2 | Chinese | 60 |
| 2 | English | 70 |
| 3 | Chinese | 80 |
| 3 | English | 50 |
+------+---------+-------+
student
+------+----------+--------+
| ID | CLASS_ID | NAME |
+------+----------+--------+
| 1 | 1 | TOM |
| 2 | 1 | ANNA |
| 3 | 2 | JOHN |
+------+----------+--------+
class
+------+----------+
| ID | NAME |
+------+----------+
| 1 | 5th |
| 2 | 6th |
+------+----------+
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
+------------+--------+
| CLASS_NAME | NAME |
+------------+--------+
| 5th | TOM |
| 6th | ANNA |
| 6th | JOHN |
+------------+--------+
mysql> select SUM(SCORE) as total from score group by ID;
+-------+
| total |
+-------+
| 140 |
| 130 |
| 130 |
+-------+
Could I merge two select sentence let it be
+------------+--------+-------+
| CLASS_NAME | NAME | total |
+------------+--------+-------+
| 5th | TOM | 140 |
| 6th | ANNA | 130 |
| 6th | JOHN | 130 |
+------------+--------+-------+
Or is there any better search sentence to do this well?
I try use two sentence to merge , but can't have a good idea.
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
mysql> select SUM(SCORE) as total from score group by ID;
hope it can be merge success or have another answer to do this well.
You can use inner join to merge the second query. Below query takes in consideration that the join condition will be score.id with student.id
select c.name as class_name,
st.name ,
sc.total
from class c
inner join student st on st.class_id = c.id
inner join ( select id,
SUM(SCORE) as total
from score
group by id
) as sc on sc.id=st.id ;
https://dbfiddle.uk/tauTGBFO

COUNT rows in 3 tables including zero values with MySQL

I have a MySQL 5.6 database with 3 tables:
job_offer
+----+----------+-------------+-----------+
| id | name | position_id | status_id |
+----+----------+-------------+-----------+
| 1 | John | 1 | 4 |
| 2 | Smith | 1 | 4 |
| 3 | Williams | 2 | 2 |
+----+----------+-------------+-----------+
position
+----+----------+
| id | name |
+----+----------+
| 1 | frontend |
| 2 | backend |
+----+----------+
status
+----+-----------+
| id | name |
+----+-----------+
| 1 | contacted |
| 2 | declined |
| 3 | rejected |
| 4 | interview |
+----+-----------+
I would like to build a query that can count all job offers by their position and statuses.
I have this query that performs almost the way I want it:
SELECT
position.name AS position_name,
status.name AS status_name
COUNT(job_offer.id) AS offers
FROM
job_offer
LEFT OUTER JOIN
position
ON job_offer.position_id = position.id
LEFT OUTER JOIN
status
ON job_offer.status_id = status.id
GROUP BY
position_name, status_name
Which gives me this result:
+---------------+-------------+--------+
| position_name | status_name | offers |
+---------------+-------------+--------+
| frontend | interview | 2 |
| backend | declined | 1 |
+---------------+-------------+--------+
The only problem is that I also need to display all existing statuses related to positions regardless of being NULL. So ideally it should look like this:
+---------------+-------------+--------+
| position_name | status_name | offers |
+---------------+-------------+--------+
| frontend | contacted | 0 |
| frontend | declined | 0 |
| frontend | rejected | 0 |
| frontend | interview | 2 |
| backend | contacted | 0 |
| backend | declined | 1 |
| backend | rejected | 0 |
| backend | interview | 0 |
+---------------+-------------+--------+
Is it possible to achieve this with one query? Thanks in advance for any help.
We can use a cross join approach between the position and status table to generate all possible combinations. Then, left join to job_offer and aggregate by position and status to find the counts:
SELECT
p.name AS position_name,
s.name AS status_name,
COUNT(jo.id) AS offers
FROM position p
CROSS JOIN status s
LEFT JOIN job_offer jo
ON jo.position_id = p.id AND
jo.status_id = s.id
GROUP BY
p.name,
s.name
ORDER BY
p.name,
s.name;
Demo

SQL Query: How to get data from three different tables

I am using mysql and here is the schema that I have.
First Table: Domains
+-----------+--------------------+---------------+
| domain_id | domain_name | campaign_name |
+-----------+--------------------+---------------+
| 1 | test.org | campaign 1 |
| 2 | example.org | campaign 2 |
+-----------+--------------------+---------------+
Second Table: Users
+---------+-----------------+---------------+
| user_id | first_ame | last_name |
+---------+-----------------+---------------+
| 1 | John | Zimmer |
| 2 | Brian | Roberts |
| 3 | Jon | McNeill |
| 4 | Chris | Lambert |
| 5 | Vipul | Patel |
| 6 | Logan | Green |
+---------+-----------------+---------------+
Third Table: Emails
+----------+----------------------------------+-----------+---------+
| email_id | email | domain_id | user_id |
+----------+----------------------------------+-----------+---------+
| 1 | b1#test.org | 1 | 2 |
| 2 | b2#test.org | 1 | 1 |
| 3 | a1#example.org | 2 | 2 |
| 4 | a2#example.org | 2 | 3 |
| 5 | a3#example.org | 2 | 3 |
| 6 | a4#example.org | 2 | 4 |
+----------+----------------------------------+-----------+---------+
I want to get first_name, last_name and email of specific campaign i-e campaign 2 as shown follow.
Here is Online DB Query Editor
Kindly guide me how can I write SQL query to accomplish that. Thanks
If I am not missing anything, this is basically a join. You have the ids nicely matched between the tables, so you can do:
SELECT u.*, e.email, d.campaign_name
FROM Users u JOIN
Emails e
ON u.user_id = e.user_id JOIN
Domains d
ON e.domain_id = d.domain_id
WHERE d.campaign_name = 'campaign 2';
The email table is a so called bridge table between the other two. You have to perform a join between the three tables:
SELECT first_name, last_name, email
FROM Domains JOIN Emails ON Domains.domain_id = Emails.domain_id JOIN Users ON Emails.user_id = Users.user_id
WHERE Domains.campaign_name = ...
SELECT first_name as 'First Name',last_name as 'Last Name', email as 'email ID',campaign_name as 'Compaign Name'
FROM Users u
inner join Emails e
on e.user_id = u.user_id
inner join Domains d
on d.domain_id = e.domain_id

school work delivery list mysql

I have two tables:
Table students and table of school work delivered
Students table
+--------------------------+---------------------------------+
| id | name |
+--------------------------+---------------------------------+
| 1 | ADAM |
| 2 | BRIGITTE |
| 3 | ANNE |
+--------------------------+---------------------------------+
table student works
+---------------+-------------------------+------------------+
| id_works | works | id_student |
+---------------+-------------------------+------------------+
| 1 | airplane wing | 1 |
| 2 | volcano | 2 |
| 3 | law of gravity | 1 |
| 4 | airplane wing | 3 |
| 5 | law of gravity | 1 |
+-----------------------------------------+------------------+
How do I make a SELECT for work that returns the entire list of students, indicating that the work is delivered? (IMPORTANT: list of all students)
Example
LIST FOR WORK **airplane wing**
+--------------------------+---------------------------------+
| ADAM | X |
| BRIGITTE | |
| ANNE | X |
+--------------------------+--------------------- -----------+
I have tried it with LEF JOIN and IF, but it is not the list of all the students without repeating them.
SELECT
s.name ,
w.work,
w.resid_id,
if(w.work = 'airplane wing', 'X', '') as mark
FROM students s
LEFT JOIN works w
ON s.id = w.id_student
ORDER BY s.name ASC
This will give you a list of all students
And fields id_works and works will be null for those who didn't complete the work
SELECT s.name, w.id_works, w.works
FROM students s
LEFT JOIN works w
ON (w.id_student = s.id AND w.works = 'airplane wing')
ORDER BY s.name ASC

Left join with count return one result

I have two tables.
|Table One: Adversitements-----------------------|
| ID | ADVTITLE |
|----|-------------------------------------------|
| 1 | IT Staff will be taken. |
| 2 | Human resources personnel will be taken. |
| 3 | CNC Operator will be taken. |
|Table Two: Applications-----|
| ID | ADVID | APPLICANTNAME |
|----|-------|---------------|
| 1 | 1 | John Doe |
| 2 | 1 | John Doe 2 |
| 3 | 1 | Jane Doe |
| 4 | 2 | John Doe |
| 5 | 2 | Jane Doe |
| 6 | 3 | John Doe |
I Want result:
| ADVTITLE | APPLICANTCOUNT |
|-------------------------------------------|----------------|
| IT Staff will be taken. | 3 |
| Human resources personnel will be taken. | 2 |
| CNC Operator will be taken. | 1 |
But returning a single result;
OUTPUT:
| ADVTITLE | APPLICANTCOUNT |
|-------------------------|----------------|
| IT Staff will be taken. | 6 |
MySQL Query;
SELECT adv.advtitle, COUNT(applications.id) as applicantCount
FROM advertisements as adv
LEFT JOIN applications
ON adv.id = applications.advid
All listings can be related to how the number of applicants?
SQL Fiddle Link: http://sqlfiddle.com/#!2/8644c/1/0
You missed the GROUP BY clause:
SELECT adv.advtitle, COUNT(applications.id) as applicantCount
FROM advertisements as adv
LEFT JOIN applications ON adv.id = applications.advid
GROUP BY adv.advtitle
ORDER BY applicantCount desc
Result:
ADVTITLE APPLICANTCOUNT
---------------------------------------------------------
IT Staff will be taken. 3
Human resources personnel will be taken.. 2
CNC Operator will be taken. 1
Fiddle Example
You need GROUP BY clause
SELECT adv.advtitle, COUNT(applications.id) as applicantCount
FROM advertisements as adv
LEFT JOIN applications
ON adv.id = applications.advid
GROUP BY applications.advid
SQL Fiddle
i already test it on my own try to paste it in your sqlfiddle :)
SELECT A.advtitle,COUNT(B.advid) AS ApplicantCount FROM advertisements A
LEFT JOIN applications B ON B.advid = A.id
GROUP BY A.id