SQL need groups - sql

I want to group unique rows from 1 table that may have many rows in table 2.
If I do make this query:
SELECT c.`lastname`,a.`city`
FROM `customer` c
LEFT JOIN `visit` a ON (c.`id` = a.`id`);
I get results like this:
lastname |city
-------- |----
DOE |Paris
DOE |Miami
Technical|Toronto
Technical|d
M |toronto
mavya |TORONTO
lname |NULL
(Both customer and visit tables also have ids that I'm joining, but not showing here).
How can I get results like below? (Can I get SQL to return an array inside results?)
lastname |city
-------- |----
DOE |Paris,Miami
Technical|Toronto,d
M |toronto
mavya |TORONTO
lname |NULL

Based on your syntax, I am guessing that you are using MySQL (or SQLite).
In either, you can use GROUP_CONCAT():
SELECT c.lastname, GROUP_CONCAT(v.`city`)
FROM customer c LEFT JOIN
visit v
ON c.id = v.id
GROUP BY c.lastname;

Related

Postgres join and count multiple relational tables

I want to join the 2 tables to the first table and group by a vendor name. I have three tables listed below.
Vendors Table
| id | name
|:-----------|------------:|
| test-id | Vendor Name |
VendorOrders Table
| id | VendorId | Details | isActive(Boolean)| price |
|:-----------|------------:|:------------:| -----------------| --------
| random-id | test-id | Sample test | TRUE | 5000
OrdersIssues Table
| id | VendorOrderId| Details. |
|:-----------|--------------:-----------:|
| order-id | random-id | Sample test|
The expected output is to count how many orders belong to a vendor and how many issues belongs to a vendor order.
I have the below code but it's not giving the right output.
SELECT "vendors"."name" as "vendorName",
COUNT("vendorOrders".id) as allOrders,
COUNT("orderIssues".id) as allIssues
FROM "vendors"
LEFT OUTER JOIN "vendorOrders" ON "vendors".id = "vendorOrders"."vendorId"
LEFT OUTER JOIN "orderIssues" ON "orderIssues"."vendorOrderId" = "vendorOrders"."id"
GROUP BY "vendors".id;```
You need the keyword DISTINCT, at least for allOrders:
SELECT v.name vendorName,
COUNT(DISTINCT vo.id) allOrders,
COUNT(DISTINCT oi.id) allIssues
FROM vendors v
LEFT OUTER JOIN vendorOrders vo ON v.id = vo.vendorId
LEFT OUTER JOIN orderIssues oi ON oi.vendorOrderId = vo.id
GROUP BY v.id, v.name;
Consider using aliases instead of full table names to make the code shorter and more readable.
You are joining along two related dimensions. The overall number of rows is the number of issues. But to get the number of orders, you need a distinct count:
SELECT v.*, count(distinct vo.id) as num_orders,
COUNT(oi.vendororderid) as num_issues
FROM vendors v LEFT JOIN
vendorOrders vo
ON v.id = vo.vendorId LEFT JOIN
orderIssues oi
ON oi.vendorOrderId = vo.id
GROUP BY v.id;
Notes:
Table aliases make the query easier to write and to read.
Quoting column and table names makes the query harder to write and read. Don't quote identifiers (you may need to recreate the tables).
Postgres support SELECT v.* . . . GROUP BY v.id assuming that the id is the primary key (actually, it only needs to be unique). This seems like a reasonable assumption.

How to resolve an id by an foreign table

I want to resolve an ID by another table, where the name of this id is stored.
SELECT d.id_data, string_agg(s.name_last,', ') AS authors, d.title, i.name
FROM data d, institution i, staffs s
WHERE d.id_staffs = s.id_staffs
AND d.id_institution = i.id_institution
GROUP BY d.id_data limit 100 ;
But how can I get the name of my Institution. I want that the SELECT shows me the institution name, which has stored the data. Something like that
id_data | authors | title | name
----------------------------------------------------------------
1 |Mustermann, Musterfrau | sunmaker | university cologne
2 |Schmidt, Müller | dry age | university berlin
I just need to resolve the id of the institution to his name.
Always use proper, explicit, standard JOIN syntax. Never use commas in the FROM clause.
Presumably, you want something like this:
SELECT d.id_data, string_agg(s.name_last,', ') AS authors,
d.title, i.name
FROM data d JOIN
institution i
ON d.id_staffs = s.id_staffs JOIN
staffs s
ON d.id_institution = i.id_institution
GROUP BY d.id_data, d.title, i.name
LIMIT 100 ;
That is, fix the GROUP BY to have all the unaggregated columns.

Subquery to extract values from 4 tables

Some people told me this is not a very difficult one but unfortunately I can't really figure it out. I have the following tables
Person
**ID_Pers** | PersName | City | State | Phone | Email
1950901123893 Michael Houston Texas --- ----
Student
**ID_student** | *ID_Pers* | entry_year | status | exit_year
A123 1950901123893 2014 finished 2017
Subject
**ID_subject** | Subject_name | credit_number | courses | laboratories
Exams
*ID_student* | *ID_subject* | **exam_date** | final_grade
The bold ones are primary keys while the ones that are italicized are foreign keys. I have to extract the students that have passed at least the exams that Michael has passed. I know I have to use subqueries in which I first select the exams that Michael has passed, then select the students that have passed those exams at least. I am having difficulties with this assignment. Can anyone assist me, please?
SELECT * from Exams
INNER JOIN student ON student.ID_student = exams.ID_student
INNER JOIN person ON student.ID_Pers = person.ID_pers
WHERE exams.final_grade >=5 AND person.PersName = "Michael"
I know this is to basically select the exams that Michael has passed. But I need something before it to extract the other people which have passed at least the ones he passed.
You can fetch all the students that have passed the Exams (any exams) and then JOIN the result with the exams fetched in your query. Your final Query should look something like this:
SELECT * from Exams A
INNER JOIN student ON student.ID_student = exams.ID_student
INNER JOIN person ON student.ID_Pers = person.ID_pers
INNER JOIN
(SELECT * from Exams
INNER JOIN student ON student.ID_student = exams.ID_student
INNER JOIN person ON student.ID_Pers = person.ID_pers
WHERE Exams.final_grade >=5 AND person.PersName = "Michael") B ON A.ID_Subject = B.ID_Subject AND A.exam_date = B.exam_date
WHERE A.final_grade >=5;
Hope this helps!

Select Query Joining 3 Tables

I've 3 Tables
Personel : id, name
Department : id, name
Match_Dept_Per : dept_id, pers_id, workInfo
Foreign Keys :
dept_id --> Department.id
pers_id --> Personel.id
Example Data :
Personel :
1, Emir Civas
2, Sercan Tuncay
Department :
1, Sales
2, Planning
Match_Dept_Per :
1,1,Manager
What I'm trying to do is, listing peoples names, their department names and workInfos like:
ID | Pers. Name | Dept Name | Work Info
---------------------------------------
1 | Emir Civas | Sales | Manager
I can do this with a simple select query:
select p.id, p.name, d.name, m.workInfo
from personel p, department d, match_dept_per m
where p.id = m.pers_id and d.id = m.dept_id;
Here is sample fiddle of my schema and this query.
However what I need is to display other persons that their id's are not inserted to match_dept_per table. And Set "Unknown" As the Null Values. Like:
ID | Pers. Name | Dept Name | Work Info
------------------------------------------
1 | Emir Civas | Sales | Manager
2 | Sercan Tuncay | Unknown | Unknown
Since I'm Using Match_Dept_Per Table, If Personel ID isn't Added, I can't do anything.
Any suggestions ?
Use left outer join to include all persons even if they are not associated with the other tables:
select p.id,
p.name,
ifnull(d.name, 'Unknown') DepName,
ifnull(m.workInfo, 'Unknown') workInfo
from personel p
left outer join match_dept_per m
on p.id = m.pers_id
left outer join department d
on d.id = m.dept_id
Here is a demo fiddle.
As you seem to use MS SQL, you might need to use isnull() instead of ifnull(). But I would ommit that anyway because I think it's better to have a NULL in the code where you use the data (Java, C#, whatever). You can control the output there.

Which table exactly is the "left" table and "right" table in a JOIN statement (SQL)?

What makes a given table the left table?
Is it that the table is indicated in the "From" part of the query?
Or, is it the left table because it is on the left hand side of the = operator?
Are the following equivalent
SELECT *
FROM left_table
LEFT JOIN right_table ON left_table.right_id = right_table.id
and
SELECT *
FROM left_table
LEFT JOIN right_table on right_table.left_id = left_table.id
???
Thanks
The Left table is the first table in the select. Yes, your two examples are equivalent.
The right table is always the table that you are joining on. So yes, both of your statements are equivalent.
JOIN [Table] ON ...
[Table] is always the right table.
Roughly "left" is the result of everything that appears first in the whole FROM clause when reading from left to right - including the result of other JOINs, sub-queries, VIEWs and STORED PROCEDURES.
Both SQL statements are equivalent because the = operator at the ON part of the JOIN clause is symmetric (if a = b then b = a) so the result is the same no matter the order.
The regular join shows only the lines where the ON clause of the JOIN is true, while the LEFT JOIN shows also the records from "left" if the condition is false (showing NULL for any column from "right" present in the SELECT).
For example:
-- People: -- Car
id | name owner_id | model
---+------------ ---------+------------
1 | Paul 1 | Ferrari
2 | Nancy 2 | Porsche
3 | Arthur NULL | Lamborghini
4 | Alfred 10 | Maserati
> select people.name, car.model from people join car on car.owner_id=people.id;
name | model
---------+--------------
Paul | Ferrari
Nancy | Porsche
2 record(s) found
> select people.name, car.model from people left join car on
car.owner_id=people.id;
name | model
---------+--------------
Paul | Ferrari
Nancy | Porsche
Arthur | NULL
Alfred | NULL
4 record(s) found
> select people.name, car.model from people left join car on
people.id = car.owner_id;
name | model
---------+--------------
Paul | Ferrari
Nancy | Porsche
Arthur | NULL
Alfred | NULL
4 record(s) found
See this for a pretty good walkthrough on joins: http://en.wikipedia.org/wiki/Join_(SQL)
And yes, both statements are equivalent :-)
Yes, it's determined by the side of the JOIN operator the table appears on. Your two examples are indeed equivalent.
CREATE TABLE ORDERS (
ORDERID INT,
CUSTOMERID INT,
ORDERDATE DATE
);
INSERT INTO ORDERS VALUES (10123,10,DATE '16-08-20');
INSERT INTO ORDERS VALUES (10122,11,DATE '14-09-20');
INSERT INTO ORDERS VALUES (10121,12,DATE '10-10-20');
CREATE TABLE CUSTOMERS (
CUSTOMERID INT,
CUSTOMERNAME VARCHAR(20),
COUNTRY VARCHAR(20)
);
INSERT INTO CUSTOMERS VALUES (11 , 'BUDDHA','INDIA');
INSERT INTO CUSTOMERS VALUES (12 , 'JOHNWIK','UNITED STATES');
INSERT INTO CUSTOMERS VALUES (100, 'SERENA','UNITED KINGDOM');
discussing LEFT JOIN query:
select orders.orderid, customers.customername, orders.orderdate from orders
inner join customers on orders.customerid = customers.customerid;
If you want to know exact left and right tables. From left to right the table attached with from is [left] and table attached with join is [right].
Happy Hacking !!!