mysql> select * from Orders;
+------+------+------+------------+------------+
| ONO | CNO | ENO | RECEIVED | SHIPPED |
+------+------+------+------------+------------+
| 1020 | 1111 | 1000 | 1994-12-10 | 1994-12-12 |
| 1021 | 1111 | 1000 | 1995-01-12 | 1995-01-15 |
| 1022 | 2222 | 1001 | 1995-02-13 | 1995-02-20 |
| 1023 | 3333 | 1000 | 2003-02-15 | NULL |
| 1024 | 4444 | 1000 | 2003-02-15 | 2003-02-16 |
| 1025 | 5555 | 1000 | 2003-02-15 | 2003-02-16 |
+------+------+------+------------+------------+
mysql> select * from Employees;
+------+--------+-------+------------+
| ENO | ENAME | ZIP | HDATE |
+------+--------+-------+------------+
| 1000 | Jones | 67226 | 1995-12-12 |
| 1001 | Smith | 60606 | 1992-01-01 |
| 1002 | Brown | 50302 | 1994-09-01 |
| 1003 | Green | 28411 | 2002-09-01 |
| 1004 | Purple | 28411 | 2003-01-01 |
+------+--------+-------+------------+
mysql> select * from Customers;
+------+---------+------------------+-------+--------------+
| CNO | CNAME | STREET | ZIP | PHONE |
+------+---------+------------------+-------+--------------+
| 1111 | Charles | 123 Main St. | 67226 | 316-636-5555 |
| 2222 | Bertram | 237 Ash Avenue | 67226 | 316-689-5555 |
| 3333 | Barbara | 111 Inwood St. | 60606 | 316-111-1234 |
| 4444 | Will | 111 Kenwood St. | 54444 | 416-111-1234 |
| 5555 | Bill | 211 Marlwood St. | 28408 | 416-111-1235 |
| 6666 | Keely | 211 Pinewood St. | 28411 | 416-111-1235 |
| 7777 | Maera | 211 Marlwood St. | 28408 | 416-111-1235 |
+------+---------+------------------+-------+--------------+
I need to: Get cname and ename pairs such that the customer with name
cname has placed an order through employee with name ename.
I'm sure this seems easy to most but I have not been able to figure
this out. I have tried this:
select distinct Customers.CNAME, Employees.ENAME
from Customers, Employees
where Customers.CNO in (
select Customers.CNO
from Customers, Orders
where Customers.CNO = Orders.CNO
) and
where Employees.ENO in (
select Employees.ENO
from Orders, Employees
where Employees.ENO = Orders.ENO
);
in multiple versions, but I can't seem to get it to work. This is my first time working in SQL so it is very new to me. I would really appreciate any help.
select customers.CNAME, employees.ENAME from customers
inner join orders on customers.CNO = orders.CNO
inner join employees on orders.ENO = employees.ENO;
Using join statements makes it easier to read the SQL you are trying to do. This joins orders table to customers on the CNO value and orders table with employees on the ENO value. It then selects two columns for display.
try this
SELECT DISTINCT Customers.CNAME,Employees.ENAME FROM Customers
JOIN Orders ON Customers.CNO=Orders.CNO
JOIN Employees ON Employees.ENO=Orders.ENO;
or
SELECT DISTINCT Customers.CNAME,Employees.ENAME from Customers
JOIN Orders USING (CNO)
JOIN Employees USING(ENO);
You could use a select distinc with a pair of join ( left join if could be that the on condition don't match or inner join if ever match )
select distinct c.cname, e.ename
from Customers as c
left join Orders as o as o on o.cno = c.cno
left join Employees on e.eno = o.eno
select distinct c.cname, e.ename
from Customers as c
inner join Orders as o as o on o.cno = c.cno
inner join Employees on e.eno = o.eno
SELECT DISTINCT c.cname, e.ename
FROM orders o
JOIN employees e USING (eno)
JOIN clients c USING (cno);
Related
i'm currently stuck on a sql-query, trying to find a solution, but making me headache for 2 stays now. i've got 3 tables
user-table:
+-----+----------+-----------+
| pid | username | role |
+-----+----------+-----------+
| 1 | user1 | patient |
| 2 | user2 | patient |
| 3 | user3 | doc |
| 4 | user4 | assistant |
| 5 | user5 | patient |
+-----+----------+-----------+
base-dat:
+-----+---------+-------+------------+
| pid | surname | name | birthdate |
+-----+---------+-------+------------+
| 1 | smith | john | 1950-07-31 |
| 2 | jackson | sarah | 1948-08-15 |
+-----+---------+-------+------------+
med-dat:
+-----+-----+---------------+--------+--------+
| mid | pid | dateLastEntry | weight | pulse |
+-----+-----+---------------+--------+--------+
| 1 | 1 | 2017-12-01 | 86 | 65 |
| 2 | 1 | 2017-12-02 | 84 | 70 |
| 3 | 1 | 2017-12-03 | 80 | 67 |
| 4 | 2 | 2017-11-15 | 66 | 60 |
| 5 | 2 | 2017-11-17 | 60 | 64 |
+-----+-----+---------------+--------+--------+
I'm trying to get the max(dateLastEntry) for each user with role patient, showing their pid, name, surname, weight, pulse in a single row - , even if there is no med-data entry for the patient: something like this:
+-----+---------+-------+------------+--------+-------+
| pid | surname | name | lastEntry | weight | pulse |
+-----+---------+-------+------------+--------+-------+
| 1 | smith | john | 2017-12-02 | 84 | 70 |
| 2 | jackson | sarah | 2017-11-17 | 60 | 64 |
| 5 | NONE | NONE | NONE | NONE | NONE |
+-----+---------+-------+------------+--------+-------+
Atm my statement looks like this, but can't get the proper result:
select b.pid, s.surname, s.name, max(m.date) as lastEntry, m.weight, m.pulse
from users b
left join med-dat m on b.pid = m.pid
left join base-dat s on m.pid = s.pid
where b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;
You can rewrite your query as below to get the desired output
select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b
left join med_dat m on b.pid = m.pid
left join base_dat s on m.pid = s.pid
left join med_dat m1 on m.pid = m1.pid
and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
DEMO
Edit from comment, join base_dat using pid from user table
select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b
left join med_dat m on b.pid = m.pid
left join base_dat s on b.pid = s.pid
left join med_dat m1 on m.pid = m1.pid and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;
Demo
I have a PostgreSQL database linked to a Drill instance.
Whenever I am trying to join 2 tables which both have a column name and whenever I want to select this name Drill selects the wrong name column. What am I doing wrong?
Given the following 2 tables:
Department
| id | name |
|----|------|
| 1 | A |
| 2 | B |
Employee
| id | name | dept | salary |
|----|------|------|--------|
| 1 | U | 1 | 100 |
| 2 | V | 1 | 75 |
| 3 | W | 1 | 120 |
| 4 | X | 2 | 95 |
| 5 | Y | 2 | 140 |
| 6 | Z | 2 | 55 |
Running
select employee.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'
returns
| name | salary |
|------|--------|
| A | 100 |
| A | 75 |
| A | 120 |
Running
select dept.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'
returns
| name | salary |
|------|--------|
| null | 100 |
| null | 75 |
| null | 120 |
What does work, but seems very silly to me, is:
select dept.name, employee.salary
from employee
inner join (select id, name as deptname from department) as department on employee.dept = department.id
where department.deptname = 'A'
This seems to be because you use
select dept.name, [...]
But you have never assigned an alias for the table department (department AS dept). Hence
select department.name, [...]
should yield the value you are looking for.
Lets say that I have something table like this:
<b>Name</b>
+---------+--------+
| name_id | name |
+---------+--------+
| 5 | Betti |
| 6 | Derry |
| 7 | Alfred |
| 8 | Elsie |
| 9 | Cinta |
+---------+--------+
<b>Goods</b>
+----------+-----------+
| goods_id | goods |
+----------+-----------+
| 1 | Computer |
| 2 | AC |
| 3 | Microwave |
| 4 | TV |
+----------+-----------+
<b>Transaction</b>
+-------+---------+----------+
| ai_id | name_id | goods_id |
+-------+---------+----------+
| 1 | 7 | 2 |
| 2 | 5 | 4 |
| 3 | 9 | 3 |
+-------+---------+----------+
I want to replace name_id column on Transaction table with name column on Name table with corresponding name_id column and so for goods_id to produce something similar to this table:
<b>Transaction</b>
+-------+--------+-----------+
| ai_id | name | goods |
+-------+--------+-----------+
| 1 | Alfred | AC |
| 2 | Betti | TV |
| 3 | Cinta | Microwave |
+-------+--------+-----------+
If you're looking to just display the information rather than actually "replacing" your tables with new ones, then you could use a SELECT query with a simple INNER JOIN. This way you can display columns from multiple tables.
SELECT T.ai_id, N.Name, G.goods
FROM Transaction T
INNER JOIN Name N ON N.name_id = T.name_id
INNER JOIN Goods G ON G.goods_id = T.goods_id;
Try with inner join
SELECT T.ai_id,N.name,G.goods
FROM Transaction as T
INNER JOIN Goods as G ON T.goods_id = G.goods_id
INNER JOIN Name as N ON N.name_id = T.name_id;
Try this one
select tb3.ai_id,tb2.name,tb1.goods from Goods tb1,Name tb2,Transaction tb3 where tb3.name_id=tb2.name_id and tb3.goods_id=tb1.goods_id order by tb3.ai_id
try this:
SELECT C.ai_id,A.name,B.goods
FROM Transaction C
INNER JOIN Name A
ON A.name_id=C.name_id
INNER JOIN Goods B
ON B.goods_id=C.goods_id;
http://sqlfiddle.com/#!2/3c5f3/8
i have 3 tables, the employee table, educational background and job_title,
i need to join the 3 tables and select the latest year the employee is graduated. the result query i need is listed below.
EMPLOYEE TABLE
|ID | employee_id | Name |
| 1 | 123 | Jewel Brin |
| 2 | 554 | Donna Ferry |
| 3 | 853 | Ricky David |
educational background
|ID | employee_id | School/level | date graduated |
| 1 | 123 | highschool | 2007 |
| 2 | 123 | college | 2011 |
| 3 | 554 | college | 2010 |
| 4 | 554 | masteral | 2013 |
job title
|ID | employee_id | Job description |
| 1 | 123 | Free lancer |
| 2 | 554 | admin assistant |
| 3 | 853 | Support Admin |
i need to select the latest date info of the employee's educational background
the result would be:
result query
|ID | employee_id | Name | Job title | year_graduated | school_institute |
| 1 | 123 | Jewel Brin | Free Lancer | 2011 | college |
| 2 | 554 | Donna Ferry | Admin Assistant | 2013 | masteral |
| 3 | 853 | Ricky David | Support Admin | Null | Null
Forgot to mention, this is MySQL :)
SELECT e.employee_id, Name, `Job description` AS `Job title`, year_graduated, school_institute
FROM `employee` e
INNER JOIN job_title j ON j.employee_id = e.employee_id
LEFT JOIN (SELECT ID, `School/level` AS school_institute, employee_id, `date graduated` AS year_graduated FROM `educational background` b
INNER JOIN (
SELECT ID, MAX(`date graduated`) AS max_date FROM `educational background` GROUP BY employee_id
) b2
ON b2.max_date = b.year_graduated AND b2.ID = b.ID
) bb ON bb.employee_id = e.employee_id
In SQL Server :-
Select *
from
(
Select e.ID,e.employee_ID,e.Name,
b.Level,j.[Job description],
rn = row_number()
over (partition by e.Employee_ID order by [date graduated] desc)
From Employee e
left join Background b
on e.Employee_ID = b.Employee_ID
left join Jobtitle j
on j.Employee_ID = b.Employee_ID
)a
where a.rn=1
SQL FIDDLE
For Mysql you can try this
SELECT DISTINCT
e.employee_id,
e.Name,
j.Job_description,
eb.year_graduated,
eb.level
FROM
EMPLOYEE e
LEFT JOIN
(SELECT
*,
MAX(t.date_graduated) year_graduated
FROM
(SELECT * FROM Background ORDER BY date_graduated DESC) t
GROUP BY employee_id) eb
ON (e.employee_id = eb.employee_id)
LEFT JOIN JobTitle j
ON (e.employee_id = j.employee_id)
See Fiddle Demo
Suppose I have two tables:
Customers
Name | id |
-------------------
Benny | 1 |
Wilson | 2 |
Joe | 3 |
Austin | 4 |
Orders
Product | id |
---------------------
TV | 1 |
Hifi-set | 1 |
HTPC | 1 |
CD | 1 |
DVD | 1 |
CD | 1 |
DVD | 1 |
And this is what I want with the results:
Name | Orders |
-------------------
Benny | 7 |
Wilson | 0 |
Joe | 0 |
Austin | 0 |
I'm not familiar with SQL, But I tried:
SELECT c.Name FROM Customers AS c LEFT JOIN Orders AS o ON c.id=o.id GROUP BY c.Name
But got a wrong result:
Name | Orders |
-------------------
Benny | 4 |
Wilson | 1 |
Joe | 1 |
Austin | 1 |
What do I do?
Try:
select
c.Name,
(select count(1) from Orders where ID=c.ID)
from
Customers as c
By not using SubQuery, you can also JOIN instead.
SELECT a.Name, COUNT(b.id)
FROM Customers a LEFT JOIN Orders b
on a.ID = b.ID
GROUP BY a.Name