Who has the same job as their initial job - sql

psql (9.6.1, server 9.5.5)
hr=> select employee_id, job_id from employees;
employee_id | job_id
-------------+------------
100 | AD_PRES
101 | AD_VP
102 | AD_VP
103 | IT_PROG
104 | IT_PROG
107 | IT_PROG
124 | ST_MAN
141 | ST_CLERK
142 | ST_CLERK
143 | ST_CLERK
144 | ST_CLERK
149 | SA_MAN
174 | SA_REP
176 | SA_REP
178 | SA_REP
200 | AD_ASST
201 | MK_MAN
202 | MK_REP
205 | AC_MGR
206 | AC_ACCOUNT
(20 rows)
hr=> select employee_id, job_id from job_history;
employee_id | job_id
-------------+------------
102 | IT_PROG
101 | AC_ACCOUNT
101 | AC_MGR
201 | MK_REP
104 | ST_CLERK
124 | ST_CLERK
200 | AD_ASST
176 | SA_REP
176 | SA_MAN
200 | AC_ACCOUNT
(10 rows)
The task is from the student's guide on Oracle Database 10g. I just cloned the schema and data for PostgreSQL.
So, the task is:
Create a report that lists the employee IDs and job IDs of those employees who currently have a job title that is the same as their job title when they were initially hired by the company (that is, they changed jobs but have now gone back to doing their original job).
My solution is:
select
employee_id, job_id from employees
intersect
(select distinct on (employee_id)
employee_id, job_id from job_history
order by employee_id, start_date);
But there are published Practice Solutions for this course. And in it we can find:
SELECT
employee_id,job_id
FROM
employees
INTERSECT
SELECT
employee_id,job_id
FROM
job_history;
But their solution is not about initial hiring. Who is wrong: me or they?

Related

How to find the Effective end Date for the below table using select statement only

How to find the Effective end Date for the below table using select statement only
This is the actual table:
EMID ENAME DEPT_NO EFDT
101 ANUJ 10 1/1/2018
101 ANUJ 11 1/1/2020
101 ANUJ 12 5/1/2020
102 KUNAL 12 1/1/2019
102 KUNAL 14 1/1/2020
102 KUNAL 15 5/1/2020
103 AJAY 11 1/1/2018
103 AJAY 12 1/1/2020
104 RAJAT 10 1/1/2018
104 RAJAT 12 1/1/2020
This is desired output:
EMID ENAME DEPTNO EFDT EF_ENDT
101 ANUJ 10 1/1/2018 12/31/2019
101 ANUJ 11 1/1/2020 4/30/2020
101 ANUJ 12 5/1/2020 NULL
102 KUNAL 12 1/1/2019 12/31/2019
102 KUNAL 14 1/1/2020 4/30/2020
102 KUNAL 15 5/1/2020 NULL
103 AJAY 11 1/1/2018 12/31/2019
103 AJAY 12 1/1/2020 NULL
104 RAJAT 10 1/1/2018 12/31/2019
104 RAJAT 12 1/1/2020 NULL
The EF_ENDT needs to be populated using the statement only.
How can we do this?
This code can be generic for all Database
Basically, you want a lead and then to subtract one day. The standard SQL for this is:
select t.*,
lead(efdt) over (partition by emid order by efdt) - interval '1 day' as ef_enddt
from t;
Date/time function vary significantly among databases. All provide some method for subtracting one day. You'll probably have to adapt this to your particular (unstated) database.

How to use the value in one table to count results in another table?

I want to add a new column on my SQL query that will count the number of times a value on the row appears in a different table.
For example, let's say I have these two separate tables.
Table Name: Table_A
Id | Coach | Team_Color | Team_Number
------------------------------------------
001 | Jane | Orange | 121
002 | Frank | Purple | 232
003 | Tim | Red | 343
Table Name: Table_B
Id | Team_Number | Player_Name
----------------------------------
901 | 121 | Jimmy
902 | 121 | Wesley
903 | 121 | Samantha
904 | 121 | Wendy
905 | 232 | Tim
906 | 232 | Sean
907 | 343 | Andrew
908 | 343 | Erik
909 | 343 | Sarah
910 | 343 | Allison
911 | 343 | Desmond
912 | 343 | Kathryn
I want to end up with something like this:
Id | Coach | Team_Color | Team_Number | Player Count
--------------------------------------------------------
001 | Jane | Orange | 121 | 4
002 | Frank | Purple | 232 | 2
003 | Tim | Red | 343 | 6
The new column called "Player Count" is referencing the Team_Number value in Table_A, and counting the number of instances found on Table_B. How would I compose this into one query?
Solution to your Problem:
SELECT A.Id, A.Coach, A.Team_Color,A.Team_Number,Count(B.Id) AS Player_Count
FROM Table_A AS A
INNER JOIN Table_B B
ON A.Team_Number = B.Team_Number
GROUP BY A.Id, A.Coach, A.Team_Color,A.Team_Number;
OUTPUT:
Id Coach Team_Color Team_Number Player_Count
1 Jane Orange 121 4
2 Frank Purple 232 2
3 Tim Red 343 6
Follow the link to the demo:
http://sqlfiddle.com/#!9/9c6da4/1
EXPLAINATION:
In your problem, you have to use JOIN to join the two tables on a common Column i.e.Team_Number. Now after Joining you will get a result like this:
Id Coach Team_Color Team_Number Id Team_Number Player_Name
1 Jane Orange 121 901 121 Jimmy
1 Jane Orange 121 902 121 Wesley
1 Jane Orange 121 903 121 Samantha
1 Jane Orange 121 904 121 Wendy
2 Frank Purple 232 905 232 Tim
2 Frank Purple 232 906 232 Sean
3 Tim Red 343 907 343 Andrew
3 Tim Red 343 908 343 Erik
3 Tim Red 343 909 343 Sarah
3 Tim Red 343 910 343 Allison
3 Tim Red 343 911 343 Desmond
3 Tim Red 343 912 343 Kathryn
Now Use aggregate function COUNT on above Result to get the final result.

SQL 2008 Running average with group by

So i have a table containing below columns.
I want to compute an running average from positiondate and for example 3 days back, grouped on dealno.
I know how to do with "case by" but problem is that I have around 200 different DealNo so I do not want to write an own case by clause for every deal.
On dealNo 1 it desired output should be Average(149 243 440 + 149 224 446 + 149 243 451)
DealNo PositionDate MarketValue
1 | 2016-11-27 | 149 243 440
2 | 2016-11-27 | 21 496 418
3 | 2016-11-27 | 32 249 600
1 | 2016-11-26 | 149 243 446
2 | 2016-11-26 | 21 496 418
3 | 2016-11-26 | 32 249 600
1 | 2016-11-25 | 149 243 451
3 | 2016-11-25 | 32 249 600
2 | 2016-11-25 | 21 496 418
3 | 2016-11-24 | 32 249 600
1 | 2016-11-24 | 149 225 582
2 | 2016-11-24 | 21 498 120
1 | 2016-11-23 | 149 256 867
2 | 2016-11-23 | 21 504 181
3 | 2016-11-23 | 32 253 440
1 | 2016-11-22 | 149 256 873
2 | 2016-11-22 | 21 506 840
3 | 2016-11-22 | 32 253 440
1 | 2016-11-21 | 149 234 535
2 | 2016-11-21 | 21 509 179
3 | 2016-11-21 | 32 253 600
I tried below script but it was not very effective since my table contains around 300k rows and approx 200 different dealno.
Is there a more effective way to do this in SQL 2008?
with cte as (
SELECT ROW_NUMBER() over(order by dealno, positiondate desc) as Rownr,
dealno,
positiondate,
Currency,
MvCleanCcy
FROM T1
)
select
rownr, positiondate, DealNo, Currency,
mvcleanavg30d = (select avg(MvCleanCcy) from cte2 where Rownr between c.Rownr and c.Rownr+3)
from cte as c
You don't need window functions. You can do this using outer apply:
select t1.*, tt1.marketvalue_3day
from t1 outer apply
(select avg(tt1.marketvalue) as marketvalue_3day
from (select top 3 tt1.*
from t1 tt1
where tt1.deal1 = t1.deal1 and
tt1.positiondate <= t1.positiondate
order by tt1.positiondate desc
) tt1
) tt1;

Why the natural join's result rows count smaller than join's?

The database data is from http://www.w3resource.com/mysql-exercises/join-exercises/
sqlite> select * from employees;
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- ------------- ------------- ---------- -------------------- ------------ ------------ ---------- -------------- ---------- -------------
100 Steven King SKING 515.123.4567 1987-06-17 AD_PRES 24000 0.0 0 90
101 Neena Kochhar NKOCHHAR 515.123.4568 1987-06-18 AD_VP 17000 0.0 100 90
102 Lex De Haan LDEHAAN 515.123.4569 1987-06-19 AD_VP 17000 0.0 100 90
...
202 Pat Fay PFAY 603.123.6666 1987-09-27 MK_REP 6000 0.0 201 20
203 Susan Mavris SMAVRIS 515.123.7777 1987-09-28 HR_REP 6500 0.0 101 40
204 Hermann Baer HBAER 515.123.8888 1987-09-29 PR_REP 10000 0.0 101 70
205 Shelley Higgins SHIGGINS 515.123.8080 1987-09-30 AC_MGR 12000 0.0 101 110
206 William Gietz WGIETZ 515.123.8181 1987-10-01 AC_ACCOUNT 8300 0.0 205 110
sqlite> select * from departments;
DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
------------- ---------------------- ---------- -----------
10 Administration 200 1700
20 Marketing 201 1800
30 Purchasing 114 1700
40 Human Resources 203 2400
50 Shipping 121 1500
60 IT 103 1400
70 Public Relations 204 2700
80 Sales 145 2500
90 Executive 100 1700
100 Finance 108 1700
110 Accounting 205 1700
120 Treasury 0 1700
130 Corporate Tax 0 1700
140 Control And Credit 0 1700
150 Shareholder Services 0 1700
160 Benefits 0 1700
170 Manufacturing 0 1700
180 Construction 0 1700
190 Contracting 0 1700
200 Operations 0 1700
210 IT Support 0 1700
220 NOC 0 1700
230 IT Helpdesk 0 1700
240 Government Sales 0 1700
250 Retail Sales 0 1700
260 Recruiting 0 1700
270 Payroll 0 1700
The natural join result:
sqlite> select * from employees e natural join departments d;
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPARTMENT_NAME LOCATION_ID
----------- ------------- ------------- ---------- -------------------- ------------ ------------ ---------- -------------- ---------- ------------- ---------------------- -----------
101 Neena Kochhar NKOCHHAR 515.123.4568 1987-06-18 AD_VP 17000 0.0 100 90 Executive 1700
102 Lex De Haan LDEHAAN 515.123.4569 1987-06-19 AD_VP 17000 0.0 100 90 Executive 1700
104 Bruce Ernst BERNST 590.423.4568 1987-06-21 IT_PROG 6000 0.0 103 60 IT 1400
105 David Austin DAUSTIN 590.423.4569 1987-06-22 IT_PROG 4800 0.0 103 60 IT 1400
106 Valli Pataballa VPATABAL 590.423.4560 1987-06-23 IT_PROG 4800 0.0 103 60 IT 1400
107 Diana Lorentz DLORENTZ 590.423.5567 1987-06-24 IT_PROG 4200 0.0 103 60 IT 1400
109 Daniel Faviet DFAVIET 515.124.4169 1987-06-26 FI_ACCOUNT 9000 0.0 108 100 Finance 1700
110 John Chen JCHEN 515.124.4269 1987-06-27 FI_ACCOUNT 8200 0.0 108 100 Finance 1700
111 Ismael Sciarra ISCIARRA 515.124.4369 1987-06-28 FI_ACCOUNT 7700 0.0 108 100 Finance 1700
112 Jose Manuel Urman JMURMAN 515.124.4469 1987-06-29 FI_ACCOUNT 7800 0.0 108 100 Finance 1700
113 Luis Popp LPOPP 515.124.4567 1987-06-30 FI_ACCOUNT 6900 0.0 108 100 Finance 1700
115 Alexander Khoo AKHOO 515.127.4562 1987-07-02 PU_CLERK 3100 0.0 114 30 Purchasing 1700
116 Shelli Baida SBAIDA 515.127.4563 1987-07-03 PU_CLERK 2900 0.0 114 30 Purchasing 1700
117 Sigal Tobias STOBIAS 515.127.4564 1987-07-04 PU_CLERK 2800 0.0 114 30 Purchasing 1700
118 Guy Himuro GHIMURO 515.127.4565 1987-07-05 PU_CLERK 2600 0.0 114 30 Purchasing 1700
119 Karen Colmenares KCOLMENA 515.127.4566 1987-07-06 PU_CLERK 2500 0.0 114 30 Purchasing 1700
129 Laura Bissot LBISSOT 650.124.5234 1987-07-16 ST_CLERK 3300 0.0 121 50 Shipping 1500
130 Mozhe Atkinson MATKINSO 650.124.6234 1987-07-17 ST_CLERK 2800 0.0 121 50 Shipping 1500
131 James Marlow JAMRLOW 650.124.7234 1987-07-18 ST_CLERK 2500 0.0 121 50 Shipping 1500
132 TJ Olson TJOLSON 650.124.8234 1987-07-19 ST_CLERK 2100 0.0 121 50 Shipping 1500
150 Peter Tucker PTUCKER 011.44.1344.129268 1987-08-06 SA_REP 10000 0.3 145 80 Sales 2500
151 David Bernstein DBERNSTE 011.44.1344.345268 1987-08-07 SA_REP 9500 0.25 145 80 Sales 2500
152 Peter Hall PHALL 011.44.1344.478968 1987-08-08 SA_REP 9000 0.25 145 80 Sales 2500
153 Christopher Olsen COLSEN 011.44.1344.498718 1987-08-09 SA_REP 8000 0.2 145 80 Sales 2500
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 1987-08-10 SA_REP 7500 0.2 145 80 Sales 2500
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 1987-08-11 SA_REP 7000 0.15 145 80 Sales 2500
184 Nandita Sarchand NSARCHAN 650.509.1876 1987-09-09 SH_CLERK 4200 0.0 121 50 Shipping 1500
185 Alexis Bull ABULL 650.509.2876 1987-09-10 SH_CLERK 4100 0.0 121 50 Shipping 1500
186 Julia Dellinger JDELLING 650.509.3876 1987-09-11 SH_CLERK 3400 0.0 121 50 Shipping 1500
187 Anthony Cabrio ACABRIO 650.509.4876 1987-09-12 SH_CLERK 3000 0.0 121 50 Shipping 1500
202 Pat Fay PFAY 603.123.6666 1987-09-27 MK_REP 6000 0.0 201 20 Marketing 1800
206 William Gietz WGIETZ 515.123.8181 1987-10-01 AC_ACCOUNT 8300 0.0 205 110 Accounting 1700
sqlite> select count(*) from employees e natural join departments d;
count(*)
----------
32
The join result:
sqlite> select * from employees e join departments d using
(department_id);
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
----------- ------------- ------------- ---------- -------------------- ------------ ------------ ---------- -------------- ---------- ------------- ---------------------- ---------- -----------
100 Steven King SKING 515.123.4567 1987-06-17 AD_PRES 24000 0.0 0 90 Executive 100 1700
101 Neena Kochhar NKOCHHAR 515.123.4568 1987-06-18 AD_VP 17000 0.0 100 90 Executive 100 1700
102 Lex De Haan LDEHAAN 515.123.4569 1987-06-19 AD_VP 17000 0.0 100 90 Executive 100 1700
103 Alexander Hunold AHUNOLD 590.423.4567 1987-06-20 IT_PROG 9000 0.0 102 60 IT 103 1400
104 Bruce Ernst BERNST 590.423.4568 1987-06-21 IT_PROG 6000 0.0 103 60 IT 103 1400
105 David Austin DAUSTIN 590.423.4569 1987-06-22 IT_PROG 4800 0.0 103 60 IT 103 1400
106 Valli Pataballa VPATABAL 590.423.4560 1987-06-23 IT_PROG 4800 0.0 103 60 IT 103 1400
107 Diana Lorentz DLORENTZ 590.423.5567 1987-06-24 IT_PROG 4200 0.0 103 60 IT 103 1400
108 Nancy Greenberg NGREENBE 515.124.4569 1987-06-25 FI_MGR 12000 0.0 101 100 Finance 108 1700
109 Daniel Faviet DFAVIET 515.124.4169 1987-06-26 FI_ACCOUNT 9000 0.0 108 100 Finance 108 1700
110 John Chen JCHEN 515.124.4269 1987-06-27 FI_ACCOUNT 8200 0.0 108 100 Finance 108 1700
111 Ismael Sciarra ISCIARRA 515.124.4369 1987-06-28 FI_ACCOUNT 7700 0.0 108 100 Finance 108 1700
112 Jose Manuel Urman JMURMAN 515.124.4469 1987-06-29 FI_ACCOUNT 7800 0.0 108 100 Finance 108 1700
...
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 1987-08-11 SA_REP 7000 0.15 145 80 Sales 145 2500
156 Janette King JKING 011.44.1345.429268 1987-08-12 SA_REP 10000 0.35 146 80 Sales 145 2500
157 Patrick Sully PSULLY 011.44.1345.929268 1987-08-13 SA_REP 9500 0.35 146 80 Sales 145 2500
158 Allan McEwen AMCEWEN 011.44.1345.829268 1987-08-14 SA_REP 9000 0.35 146 80 Sales 145 2500
159 Lindsey Smith LSMITH 011.44.1345.729268 1987-08-15 SA_REP 8000 0.3 146 80 Sales 145 2500
160 Louise Doran LDORAN 011.44.1345.629268 1987-08-16 SA_REP 7500 0.3 146 80 Sales 145 2500
161 Sarath Sewall SSEWALL 011.44.1345.529268 1987-08-17 SA_REP 7000 0.25 146 80 Sales 145 2500
162 Clara Vishney CVISHNEY 011.44.1346.129268 1987-08-18 SA_REP 10500 0.25 147 80 Sales 145 2500
163 Danielle Greene DGREENE 011.44.1346.229268 1987-08-19 SA_REP 9500 0.15 147 80 Sales 145 2500
164 Mattea Marvins MMARVINS 011.44.1346.329268 1987-08-20 SA_REP 7200 0.1 147 80 Sales 145 2500
165 David Lee DLEE 011.44.1346.529268 1987-08-21 SA_REP 6800 0.1 147 80 Sales 145 2500
166 Sundar Ande SANDE 011.44.1346.629268 1987-08-22 SA_REP 6400 0.1 147 80 Sales 145 2500
167 Amit Banda ABANDA 011.44.1346.729268 1987-08-23 SA_REP 6200 0.1 147 80 Sales 145 2500
168 Lisa Ozer LOZER 011.44.1343.929268 1987-08-24 SA_REP 11500 0.25 148 80 Sales 145 2500
169 Harrison Bloom HBLOOM 011.44.1343.829268 1987-08-25 SA_REP 10000 0.2 148 80 Sales 145 2500
170 Tayler Fox TFOX 011.44.1343.729268 1987-08-26 SA_REP 9600 0.2 148 80 Sales 145 2500
171 William Smith WSMITH 011.44.1343.629268 1987-08-27 SA_REP 7400 0.15 148 80 Sales 145 2500
172 Elizabeth Bates EBATES 011.44.1343.529268 1987-08-28 SA_REP 7300 0.15 148 80 Sales 145 2500
173 Sundita Kumar SKUMAR 011.44.1343.329268 1987-08-29 SA_REP 6100 0.1 148 80 Sales 145 2500
174 Ellen Abel EABEL 011.44.1644.429267 1987-08-30 SA_REP 11000 0.3 149 80 Sales 145 2500
175 Alyssa Hutton AHUTTON 011.44.1644.429266 1987-08-31 SA_REP 8800 0.25 149 80 Sales 145 2500
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 1987-09-01 SA_REP 8600 0.2 149 80 Sales 145 2500
177 Jack Livingston JLIVINGS 011.44.1644.429264 1987-09-02 SA_REP 8400 0.2 149 80 Sales 145 2500
179 Charles Johnson CJOHNSON 011.44.1644.429262 1987-09-04 SA_REP 6200 0.1 149 80 Sales 145 2500
180 Winston Taylor WTAYLOR 650.507.9876 1987-09-05 SH_CLERK 3200 0.0 120 50 Shipping 121 1500
181 Jean Fleaur JFLEAUR 650.507.9877 1987-09-06 SH_CLERK 3100 0.0 120 50 Shipping 121 1500
182 Martha Sullivan MSULLIVA 650.507.9878 1987-09-07 SH_CLERK 2500 0.0 120 50 Shipping 121 1500
183 Girard Geoni GGEONI 650.507.9879 1987-09-08 SH_CLERK 2800 0.0 120 50 Shipping 121 1500
184 Nandita Sarchand NSARCHAN 650.509.1876 1987-09-09 SH_CLERK 4200 0.0 121 50 Shipping 121 1500
185 Alexis Bull ABULL 650.509.2876 1987-09-10 SH_CLERK 4100 0.0 121 50 Shipping 121 1500
186 Julia Dellinger JDELLING 650.509.3876 1987-09-11 SH_CLERK 3400 0.0 121 50 Shipping 121 1500
187 Anthony Cabrio ACABRIO 650.509.4876 1987-09-12 SH_CLERK 3000 0.0 121 50 Shipping 121 1500
188 Kelly Chung KCHUNG 650.505.1876 1987-09-13 SH_CLERK 3800 0.0 122 50 Shipping 121 1500
189 Jennifer Dilly JDILLY 650.505.2876 1987-09-14 SH_CLERK 3600 0.0 122 50 Shipping 121 1500
190 Timothy Gates TGATES 650.505.3876 1987-09-15 SH_CLERK 2900 0.0 122 50 Shipping 121 1500
191 Randall Perkins RPERKINS 650.505.4876 1987-09-16 SH_CLERK 2500 0.0 122 50 Shipping 121 1500
192 Sarah Bell SBELL 650.501.1876 1987-09-17 SH_CLERK 4000 0.0 123 50 Shipping 121 1500
193 Britney Everett BEVERETT 650.501.2876 1987-09-18 SH_CLERK 3900 0.0 123 50 Shipping 121 1500
194 Samuel McCain SMCCAIN 650.501.3876 1987-09-19 SH_CLERK 3200 0.0 123 50 Shipping 121 1500
195 Vance Jones VJONES 650.501.4876 1987-09-20 SH_CLERK 2800 0.0 123 50 Shipping 121 1500
196 Alana Walsh AWALSH 650.507.9811 1987-09-21 SH_CLERK 3100 0.0 124 50 Shipping 121 1500
197 Kevin Feeney KFEENEY 650.507.9822 1987-09-22 SH_CLERK 3000 0.0 124 50 Shipping 121 1500
198 Donald OConnell DOCONNEL 650.507.9833 1987-09-23 SH_CLERK 2600 0.0 124 50 Shipping 121 1500
199 Douglas Grant DGRANT 650.507.9844 1987-09-24 SH_CLERK 2600 0.0 124 50 Shipping 121 1500
200 Jennifer Whalen JWHALEN 515.123.4444 1987-09-25 AD_ASST 4400 0.0 101 10 Administration 200 1700
201 Michael Hartstein MHARTSTE 515.123.5555 1987-09-26 MK_MAN 13000 0.0 100 20 Marketing 201 1800
202 Pat Fay PFAY 603.123.6666 1987-09-27 MK_REP 6000 0.0 201 20 Marketing 201 1800
203 Susan Mavris SMAVRIS 515.123.7777 1987-09-28 HR_REP 6500 0.0 101 40 Human Resources 203 2400
204 Hermann Baer HBAER 515.123.8888 1987-09-29 PR_REP 10000 0.0 101 70 Public Relations 204 2700
205 Shelley Higgins SHIGGINS 515.123.8080 1987-09-30 AC_MGR 12000 0.0 101 110 Accounting 205 1700
206 William Gietz WGIETZ 515.123.8181 1987-10-01 AC_ACCOUNT 8300 0.0 205 110 Accounting 205 1700
sqlite> select count(*) from employees e join departments d using (department_id);
count(*)
----------
106
The natural join result rows count should be same as join, but not, why?
The different between a natural join and a 'normal' join is that the former use all columns that happen to have the same name in both tables.
In this case, both DEPARTMENT_ID and MANAGER_ID match, so the natural join is actually the same as this query:
select * from employees e join departments d using (department_id, manager_id);
This is why you should never, ever use a natural join.

SQL server select from 3 tables

I have three tables in my database Books, Borrowers and Movement:
Books
BookID Title Author Category Published
----------- ------------------------------ ------------------------- --------------- ----------
101 Ulysses James Joyce Fiction 1922-06-16
102 Huckleberry Finn Mark Twain Fiction 1884-03-24
103 The Great Gatsby F. Scott Fitzgerald Fiction 1925-06-17
104 1984 George Orwell Fiction 1949-04-19
105 War and Peace Leo Tolstoy Fiction 1869-08-01
106 Gullivers Travels Jonathan Swift Fiction 1726-07-01
107 Moby Dick Herman Melville Fiction 1851-08-01
108 Pride and Prejudice Jane Austen Fiction 1813-08-13
110 The Second World War Winston Churchill NonFiction 1953-09-01
111 Relativity Albert Einstein NonFiction 1917-01-09
112 The Right Stuff Tom Wolfe NonFiction 1979-09-07
121 Hitchhikers Guide to Galaxy Douglas Adams Humour 1975-10-27
122 Dad Is Fat Jim Gaffigan Humour 2013-03-01
131 Kick-Ass 2 Mark Millar Comic 2012-03-03
133 Beautiful Creatures: The Manga Kami Garcia Comic 2014-07-01
Borrowers
BorrowerID Name Birthday
----------- ------------------------- ----------
2 Bugs Bunny 1938-09-08
3 Homer Simpson 1992-09-09
5 Mickey Mouse 1928-02-08
7 Fred Flintstone 1960-06-09
11 Charlie Brown 1965-06-05
13 Popeye 1933-03-03
17 Donald Duck 1937-07-27
19 Mr. Magoo 1949-09-14
23 George Jetson 1948-04-08
29 SpongeBob SquarePants 1984-08-04
31 Stewie Griffin 1971-11-17
Movement
MoveID BookID BorrowerID DateOut DateIn ReturnCondition
----------- ----------- ----------- ---------- ---------- ---------------
1 131 31 2012-06-01 2013-05-24 good
2 101 23 2012-02-10 2012-03-24 good
3 102 29 2012-02-01 2012-04-01 good
4 105 7 2012-03-23 2012-05-11 good
5 103 7 2012-03-22 2012-04-22 good
6 108 7 2012-01-23 2012-02-12 good
7 112 19 2012-01-12 2012-02-10 good
8 122 11 2012-04-14 2013-05-01 poor
9 106 17 2013-01-24 2013-02-01 good
10 104 2 2013-02-24 2013-03-10 bitten
11 121 3 2013-03-01 2013-04-01 good
12 131 19 2013-04-11 2013-05-23 good
13 111 5 2013-05-22 2013-06-22 poor
14 131 2 2013-06-12 2013-07-23 bitten
15 122 23 2013-07-10 2013-08-12 good
16 107 29 2014-01-01 2014-02-14 good
17 110 7 2014-01-11 2014-02-01 good
18 105 2 2014-02-22 2014-03-02 bitten
What is a query I can use to find out which book was borrowed by the oldest borrower?
I am new to SQL and am using Microsoft SQL Server 2014
Here are two different solutions:
First using two sub querys and one equi-join:
select Title
from Books b , Movement m
where b.BookID = m.BookID and m.BorrowerID = (select BorrowerID
from Borrowers
where Birthday = (select MIN(Birthday)
from Borrowers))
Using two equi-joins and one sub query:
select Title
from Books b, Borrowers r, Movement m
where b.BookID = m.BookID
and m.BorrowerID = r.BorrowerID
and Birthday = (select MIN(Birthday) from Borrowers)
Both above queries give the following answer:
Title
------------------------------
Relativity