Let's say I have three tables:
Employees:
PID NAME WAGE
---------- -------------------- ----------
10234 Able 8
11567 Baker 9
3289 George 10
88331 Alice 11
Employee_made:
PID SID QUANTITY HOURS
---------- ---------- ---------- ----------
10234 11 24 3
10234 12 6 1
10234 13 24 1
10234 21 6 1
10234 23 4 1
10234 31 48 6
11567 23 4 1
11567 31 1 1
88331 11 6 1
Sandwich:
SID PRICE NAME
---------- ---------- ------------------------------
12 2 hamburger on wheat
13 2 cheese burger
21 1.75 fish burger on rye
23 1.75 fish burger on wheat
31 3 veggie burger on wheat
11 2 hamburger on rye
I need to list all the employees who have made ALL the different sandwiches, and display their names and PID. What I've gotten so far is:
Select E.name, E.pid
From employees E, employee_made EM, sandwich S
Where E.pid = EM.pid
Which tells me the matching PIDs from the employees and employee_made table. Where I'm not sure to go is how to display the employees who have made ALL the sandwiches, so matching not any SID to the employee_made table, but ALL of them.
First, never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
You can approach this by counting the number of sandwiches mades by employees and then comparing to the total count of sandwiches:
select em.pid
from employee_made em
group by em.pid
having count(distinct em.sid) = (select count(*) from sandwich);
This gives the pid of the employee. I'll let you figure out how to bring in the employee name (hint: in, exists, and join could all be used).
Related
I have two tables
customers_bought:
PID SID QUANTITY
---------- ---------- ----------
3289 11 12
74591 11 10
74591 12 1
74591 23 1
74591 31 1
88331 13 21
88331 31 48
customers:
PID NAME
---------- --------------------
3289 George
88331 Alice
74591 Jane
10234 Able
What I need to be able to do is list the customers who bought the greatest number of sandwiches of a single type. I also need to display the customer name, pid, sid, and quantity. This being SQL, of course it's more complicated than it sounds. I'm unsure how to join the two results I have below, here's what I have managed to do:
Code:
Select C.pid, C.name, CB.sid, CB.quantity
From customers C
Inner Join customers_bought CB
On CB.PID = C.PID;
Result:
PID NAME SID QUANTITY
---------- -------------------- ---------- ----------
3289 George 11 12
74591 Jane 11 10
74591 Jane 12 1
74591 Jane 23 1
74591 Jane 31 1
88331 Alice 13 21
88331 Alice 31 48
Code:
Select CB.sid, MAX(CB.Quantity)
From customers_bought CB
Group By CB.sid
Result:
SID MAX(CB.QUANTITY)
---------- ---------------------------------------
11 12
13 21
31 48
23 1
12 1
Try this one
Select
C.pid, C.name, CB.sid, ms.MaxQuentity
From customers C
Inner Join customers_bought CB
On CB.PID = C.PID
INNER JOIN
(
Select
CB.sid,
MAX(CB.Quantity) AS MaxQuentity
From customers_bought CB
Group By CB.sid
) ms ON ms.sid = CB.sid AND ms.MaxQuentity = CB.QUANTITY
You can find the Sample code in Fiddle
I am currently working with three tables where I am trying to figure out how to use a join to display once the title_id of any book with Dennis McCann as an editor. The tables have in common title_id and editor_id. Cant find a way to piece it all together. How to display once the title_id of any book with Dennis McCann as an editor?
SELECT * FROM title_editors;
EDITOR_ID TITLE_ EDITOR_ORDER
----------- ------ ------------
826-11-9034 Bu2075 2
826-11-9034 PS2091 2
826-11-9034 Ps2106 2
826-11-9034 PS3333 2
826-11-9034 PS7777 2
826-11-9034 pS1372 2
885-23-9140 MC2222 2
885-23-9140 MC3021 2
885-23-9140 Tc3281 2
885-23-9140 TC4203 2
885-23-9140 TC7777 2
321-55-8906 bU1032 2
321-55-8906 BU1111 2
321-55-8906 BU7832 2
321-55-8906 PC1035 2
321-55-8906 PC8888 2
321-55-8906 BU2075 3
777-02-9831 pc1035 3
777-02-9831 PC8888 3
943-88-7920 BU1032 1
943-88-7920 bu1111 1
943-88-7920 BU2075 1
943-88-7920 BU7832 1
943-88-7920 PC1035 1
943-88-7920 pc8888 1
993-86-0420 PS1372 1
993-86-0420 PS2091 1
993-86-0420 PS2106 1
993-86-0420 PS3333 1
993-86-0420 pS7777 1
993-86-0420 MC2222 1
993-86-0420 MC3021 1
993-86-0420 Tc3218 1
993-86-0420 TC4203 1
993-86-0420 TC7777 1
35 rows selected.
SQL> SELECT * FROM title_authors;
AUTHOR_ID TITLE_ AUTHOR_ORDER ROYALTY_SHARE
----------- ------ ------------ -------------
409-56-7008 Bu1032 1 .6
486-29-1786 PS7777 1 1
486-29-1786 pC9999 1 1
712-45-1867 MC2222 1 1
172-32-1176 Ps3333 1 1
213-46-8915 BU1032 2 .4
238-95-7766 PC1035 1 1
213-46-8915 Bu2075 1 1
998-72-3567 pS2091 1 .5
899-46-2035 PS2091 2 .5
998-72-3567 PS2106 1 1
722-51-5454 mc3021 1 .75
899-46-2035 MC3021 2 .25
807-91-6654 tC3218 1 1
274-80-9391 BU7832 1 1
427-17-2319 pC8888 1 .5
846-92-7186 PC8888 2 .5
756-30-7391 PS1372 1 .75
724-80-9391 PS1372 2 .25
724-80-9391 bu1111 1 .6
267-41-2394 bU1111 2 .4
672-71-3249 TC7777 1 .4
267-41-2394 TC7777 2 .3
472-27-2349 Tc7777 3 .3
648-92-1872 TC4203 1 1
25 rows selected.
SQL> SELECT * FROM editors;
EDITOR_ID EDITOR_LNAME EDITOR_FNAME EDITOR_POSITION PHONE ADDRESS CITY ST ZIP
----------- ----------------- ------------- --------------- ------------ -------------------- ------------ -- ------
321-55-8906 DeLongue Martinella Project 415 843-2222 3000 6th St. BERKELEY Ca 94710
723-48-9010 Sparks MANfred cOPY 303 721-3388 15 Sail DENVER Co 80237
777-02-9831 Samuelson Bernard proJect 415 843-6990 27 Yosemite OAKLAND Ca 94609
777-66-9902 Almond Alfred copy 312 699-4177 1010 E. DeVON CHICAGO Il 60018
826-11-9034 Himmel Eleanore pRoject 617 423-0552 97 Bleaker BOSTON Ma 02210
885-23-9140 Rutherford-Hayes Hannah PROJECT 301 468-3909 32 Rockbill Pike ROCKBILL MD 20852
993-86-0420 McCann Dennis acQuisition 301 468-3909 32 Rockbill Pike ROCKBill MD 20852
943-88-7920 Kaspchek Christof acquisitiOn 415 549-3909 18 Severe Rd. BERKELEY CA 94710
234-88-9720 Hunter Amanda acquisition 617 432-5586 18 Dowdy Ln. BOSTON MA 02210
You can try join on the table Editors and Ttile_Editors using the Editor_ID that will give you the matching records and you can filter out Only for the ' Dennis McCann ' using either multiple conditions in join or the where clause as,
WITHOUT WHERE
SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
FROM
title_editors te JOIN editors ed
ON te.EDITOR_ID = ed.EDITOR_ID
AND ed.EDITOR_LNAME = 'McCann'
AND ed.EDITOR_FNAME = 'Dennis'
ORDER BY te.title_id
USing WHERE
SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
FROM
title_editors te JOIN editors ed
ON te.EDITOR_ID = ed.EDITOR_ID
WHERE
ed.EDITOR_LNAME = 'McCann'
AND ed.EDITOR_FNAME = 'Dennis'
ORDER BY te.title_id
It would be easier with the in operator:
SELECT DISTINCT title_id
FROM title_editors
WHERE editor_id IN (SELECT editor_id
FROM editors
WHERE editor_fname = 'Dennis' AND
editor_lname = 'McCann')
ORDER BY title_id ASC
I am trying to produce a list of branches that haven't made any sales in the last 3 years. I have been able to produce a list of sales that are older than 3 years but not with the added condition of 0 sales in the 3 years prior.
My task is as follows: List all of the branches that have not rented out any tools for more than 3 years.
I think that I have to do a nested subquery but I cannot work out what should go where. Here are the two relevant tables with their descriptions and data values. The only value that should be returned is that for branch 70.
SQL> desc toolorder
Name Null? Type
--------------------------------------------------------- -------- ---
ORDERID NOT NULL VARCHAR2(6)
CUST NOT NULL VARCHAR2(6)
SNAME NOT NULL VARCHAR2(20)
BRANCHID NOT NULL VARCHAR2(6)
TYPE NOT NULL VARCHAR2(15)
TOOLID NOT NULL VARCHAR2(6)
DATEOUT NOT NULL DATE
DUEDATE NOT NULL DATE
SQL> desc branch
Name Null? Type
--------------------------------------------------------------
BRANCHID NOT NULL VARCHAR2(6)
BNAME NOT NULL VARCHAR2(15)
ADDRESS NOT NULL VARCHAR2(25)
TELEPHONE VARCHAR2(11)
MANAGERID VARCHAR2(6)
SQL> select * from toolorder;
ORDERI CUSTOM SNAME BRANCH TYPE TOOLID DATEOUT DUEDATE
------ ------ -------------------- ------ --------------- ------ --------- ---------
000001 000100 smith 10 Adhesive 00042 20-OCT-13 27-NOV-12
000002 000101 jones 10 Guage 00050 13-OCT-12 30-OCT-12
000003 000103 may 10 Generic 00023 21-NOV-12 28-NOV-12
000004 000100 smith 10 Generic 00023 19-NOV-13 28-NOV-13
000005 000104 circus 10 Generic 00023 05-JAN-09 28-JAN-09
000006 000106 hanks 10 Wood 00062 11-APR-10 01-MAY-10
000007 000102 bond 20 Cutting 00073 13-DEC-11 27-DEC-11
000008 000102 bond 20 Guage 00053 13-DEC-11 27-DEC-11
000009 000104 circus 30 Generic 00025 13-DEC-06 28-DEC-06
000010 000104 circus 30 Brickwork 00035 13-DEC-06 28-DEC-06
000011 000105 harris 30 Cutting 00075 13-OCT-13 25-OCT-13
000012 000105 harris 40 Brickwork 00036 13-DEC-11 27-DEC-11
000013 000105 harris 40 Generic 00027 13-DEC-11 27-DEC-11
000014 000105 harris 40 Electric 00006 13-DEC-11 27-DEC-11
000015 000106 hanks 40 Adhesive 00046 13-MAY-11 27-MAY-11
000016 000107 head 50 Adhesive 00047 13-MAR-13 27-MAR-13
000017 000107 head 50 Wood 00018 13-MAR-13 27-MAR-13
000018 000101 jones 50 Guage 00055 06-JAN-13 20-JAN-13
000019 000103 may 60 Brickwork 00039 06-APR-13 20-APR-13
000020 000101 jones 60 Cutting 00080 24-DEC-12 07-JAN-13
000021 000101 circus 70 Cutting 00081 13-AUG-08 27-AUG-08
21 rows selected.
SQL> select * from branch;
BRANCH BNAME ADDRESS TELEPHONE MANAGE
------ --------------- ------------------------- ----------- ------
10 Oxford 18 Oxford Estate 08456325312
20 Greenwood 21 Greenwood Lane 02380282185
30 Weston 36 Weston Road 02380282635
40 Highstreet 12-15 Stafford Highstreet 02380865963
50 Meadow 16 The Meadow Yard 07974296353
60 Port Down 168 Port Down Av 08953164826
70 Red Rd 12-15 Red Road 07948247384
7 rows selected.
Now, running the following query returns the orders that are 3 years old. I need to adjust it (I think) using a nested subqueries, so that it checks there are no sales in the 3 years, but cannot work out how.
SQL> select count(toolorder.orderid) as rentalcount, branch.branchid, branch.bname,
branch.address from toolorder left outer join branch on toolorder.branchid =
branch.branchid where MONTHS_BETWEEN(sysdate, dateout) > 36 group by branch.branchid,
branch.bname, branch.address order by 1 desc;
RENTALCOUNT BRANCH BNAME ADDRESS
----------- ------ --------------- -------------------------
2 30 Weston 36 Weston Road
2 10 Oxford 18 Oxford Estate
1 70 Red Rd 12-15 Red Road
The easiest way to do this is to get the maximum dateout for each branchid and check that it is more than 36 months in the bast:
select b.*
from branch b join
(select branchid, max(dateout) as maxd
from toolorder
group by branchid
) tob
on b.branchid = tob.branchid
where MONTHS_BETWEEN(sysdate, tob.maxd) > 36;
You can use NOT EXISTS to check if something does not match in a correlated sub-query:
SELECT *
FROM branch b
WHERE NOT EXISTS ( SELECT 1
FROM toolorder r
WHERE r.branchid = b.branchid
AND MONTHS_BETWEEN(sysdate, dateout) <= 36 );
From your comment on #Gordon Linoff's answer, it looks like you want to delete matching rows; in which case you can do:
DELETE FROM branch b
WHERE NOT EXISTS ( SELECT 1
FROM toolorder r
WHERE r.branchid = b.branchid
AND MONTHS_BETWEEN(sysdate, dateout) <= 36 );
As defined here, the maximum number of compound select term is SQLITE_MAX_COMPOUND_SELECT
How can we get this value when we query a sqlite database using a sql command?
e.g.
select SQLITE_MAX_COMPOUND_SELECT from SomeTableSomeWhere
or
select someFunctionForThisValue()
well, I can not answer your directly to your question, but as we know SQLITE_MAX_COMPOUND_SELECT default defined 500. We can go around with this by using SELECT LIMIT, OFFSET statement to fetch all data in a large table (hope this is want you want)
for example:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
With help of count rows
sqlite> SELECT count(*) FROM COMPANY
We can select limit rows with offset to fetch all database table
sqlite> SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
reference here: http://www.tutorialspoint.com/sqlite/sqlite_limit_clause.htm
I have database table that I am after some SQL for (Which is defeating me so far!)
Imagine there are 192 Athletic Clubs who all take part in 12 Track Meets per season.
So that is 2304 individual performances per season (for example in the 100Metres)
I would like to find the top 48 (unique) individual performances from the table, these 48 athletes are then going to take part in the end of season World Championships.
So imagine the 2 fastest times are both set by "John Smith", but he can only be entered once in the world champs. So i would then look for the next fastest time not set by "John Smith"... so on and so until I have 48 unique athletes..
hope that makes sense.
thanks in advance if anyone can help
PS
I did have a nice screen shot created that would explain it much better. but as a newish user i cannot post images.
I'll try a copy and paste version instead...
ID AthleteName AthleteID Time
1 Josh Lewis 3 11.99
2 Joe Dundee 4 11.31
3 Mark Danes 5 13.44
4 Josh Lewis 3 13.12
5 John Smith 1 11.12
6 John Smith 1 12.18
7 John Smith 1 11.22
8 Adam Bennett 6 11.33
9 Ronny Bower 7 12.88
10 John Smith 1 13.49
11 Adam Bennett 6 12.55
12 Mark Danes 5 12.12
13 Carl Tompkins 2 13.11
14 Joe Dundee 4 11.28
15 Ronny Bower 7 12.14
16 Carl Tompkin 2 11.88
17 Nigel Downs 8 14.14
18 Nigel Downs 8 12.19
Top 4 unique individual performances
1 John Smith 1 11.12
3 Joe Dundee 4 11.28
5 Adam Bennett 6 11.33
6 Carl Tompkins 2 11.88
Basically something like this:
select top 48 *
from (
select athleteId,min(time) as bestTime
from theRaces
where raceId = '123' -- e.g., 123=100 meters
group by athleteId
) x
order by bestTime
try this --
select x.ID, x.AthleteName , x.AthleteID , x.Time
(
select rownum tr_count,v.AthleteID AthleteID, v.AthleteName AthleteName, v.Time Time,v.id id
from
(
select
tr1.AthleteName AthleteName, tr1.Time time,min(tr1.id) id, tr1.AthleteID AthleteID
from theRaces tr1
where time =
(select min(time) from theRaces tr2 where tr2.athleteId = tr1.athleteId)
group by tr1.AthleteName, tr1.AthleteID, tr1.Time
having tr1.Time = ( select min(tr2.time) from theRaces tr2 where tr1.AthleteID =tr2.AthleteID)
order by tr1.time
) v
) x
where x.tr_count < 48