SQL: Select groups that do not contain a certain value - sql

I use Microsoft SQL Server Management Studio 2014 and I have these 3 tables:
EMPLOYEES
EMPID | FIRSTNAME
1 | JOHNNY
2 | DWAYNE
3 | TOM
4 | CHRISTIAN
5 | JACK
6 | BRAD
7 | ADAM
8 | MATT
9 | WILL
10 | JIM
AIRCRAFTS
AID | NAME
1 | BOEING 1
2 | BOEING 2
3 | BOEING 3
4 | BOEING 4
5 | AIRBUS 1
6 | AIRBUS 2
7 | LEARJET
8 | DOUGLAS
9 | JUMBO
10 | ILYUSHIN
CERTIFIED
EMPID | AID
1 | 1
1 | 2
1 | 3
1 | 4
4 | 2
4 | 3
7 | 1
7 | 2
7 | 5
7 | 6
8 | 7
8 | 8
8 | 9
2 | 10
2 | 1
2 | 9
3 | 10
5 | 8
5 | 9
The concept is that there are 10 employees and 10 aircrafts. The CERTIFIED table determines which employee is authorized to pilot which aircrafts. Not all employees are pilots though. What I need is to somehow select all pilots who are not certified to use a Boeing. What I tried but did not work is the following:
SELECT DISTINCT FIRSTNAME
FROM EMPLOYEES
WHERE EMPID IN (SELECT EMPID
FROM CERTIFIED
WHERE AID NOT IN (SELECT AID FROM AIRCRAFTS WHERE NAME LIKE 'BOEING%'))
Which gives these results:
JACK
MATT
TOM
ADAM
DWAYNE
This is wrong because according to the CERTIFIED table, ADAM and DWAYNE are authorized to pilot at least one Boeing.
Any help would be appreciated, thanks in advance!

Try this query...
SELECT employees.empid, Max(employees.firstname) AS FirstName
FROM certified
INNER JOIN employees ON employees.empid = certified.empid
WHERE certified.empid NOT IN (SELECT certified.empid
FROM certified
INNER JOIN aircrafts ON aircrafts.aid = certified.aid
WHERE aircrafts.NAME LIKE 'BOEING%')
GROUP BY employees.empid
Demo: http://www.sqlfiddle.com/#!18/8f26d/27/0
Result
+-------+-----------+
| EMPID | FirstName |
+-------+-----------+
| 3 | TOM |
| 5 | JACK |
| 8 | MATT |
+-------+-----------+

I think your query is giving any employee that is certified on a non-Boeing aircraft -- a subtly different set of people.
For your question, I would go for not exists:
select e.*
from employees e
where not exists (select 1
from certified c join
aircrafts a
on c.aid = a.aid
where e.empid = c.empid and a.name like '%BOEING%'
);
Another approach -- if you just want the employee id -- uses aggregation and having
select e.empid, e.firstname
from employees e join
certified c
on e.empid = c.empid join
aircrafts a
on c.aid = a.aid
group by e.empid, e.firstname
having sum(case when a.name like '%BOEING%' then 1 else 0 end) = 0;
I happen to like this method, because it generalizes very easily to other conditions -- such as flies Boeing but not Airbus or flies Learjet and Cessna.

You should use NOT IN the emp that are certified by Boeing in join with aircrafts
SELECT DISTINCT FIRSTNAME
FROM EMPLOYEES
WHERE EMPID NOT IN (SELECT EMPID
FROM CERTIFIED c
INNER JOIN AIRCRAFTS a ON on a.AID = c.AID
WHERE a.NAME LIKE 'BOEING%')

Related

Sql join does not return some rows with groupBy

I am trying to learn sql.I do some practices.I created a table which called Student.
Id | Name | Amount
1 | Jone | 100
2 | Jack | 200
3 | Emily | 300
4 |Haaland | 500
7 |Ted | 700
I also created Orders table like that:
Id | Name | Amount | Dıscount
1 | Jone | 100 | 10
2 | Jack | 112 | 20
3 | Emily | 300 | 30
4 |Haaland | 500 | 50
5 |Jack | 88 | 12
7 |Ted | 150 | 235
My query is:
select a1.Id Id ,a1.Name Name, a1.Amount Amount , sum(a2.discount)
from student a1
left outer join orders a2
on a1.Id=a2.Id
and a1.Name=a2.Name
and a1.Amount = a2.Amount
group by a1.Id, a1.Name, a1.Amount
Result:
Id | Name | Amount | Dıscount
1 | Jone | 100 | 10
3 | Emily | 300 | 30
4 |Haaland | 500 | 50
2 | Jack | 200 | null
7 | Ted | 700 | null
I get null value for the jack row.I have to use a1.Amount=a2.Amount because I remove amount constraint Ted'discount also appears.
Expected Result :
Id | Name | Amount | Dıscount
1 | Jone | 100 | 10
3 | Emily | 300 | 30
4 |Haaland | 500 | 50
2 | Jack | 200 | 32
7 | Ted |700 | null
I think the logic you want is to pre-aggregate the orders of each name in a subquery, then join by name and amount:
select s.id , s.name, s.amount, o.discount
from student s
left join (
select name, sum(amount) amount, sum(discount) discount
from orders
group by name
) o on o.name = s.name and o.amount = s.amount
What is the confusion? In one row you have:
id name amount
2 Jack 200
And in the other:
id name amount
2 Jack 112
Your join requires equality on all three columns. The amounts don't match, so there is no match for Jack's row and the amount is null.
Your question is not clear on what you actually want to do, so I'll stop here.
The amount for Jack does not match (200 in Student, 88 and 112 in Orders), so nothing can be joined ON a1.Amount = a2.Amount for that record. However, Please be advised that even if one of the values in Amount does match, the GROUP BY function will still not know which Amount you want associated with 'Jack'.

Aggregate data for 2 tables sql

I have 2 questions which I was unable to answer in an interview, and hence do not have the answer.
2 questions are:
a. Write a query to get close rate by make and partner name (close rate = sales count / lead count)
Attempt:
select (totalsales/totalleads) as close_rate
(select count(sale_id) as totalsales from daily_sales group by make_name) S
outer join
(select count(lead_id) as totalleads from daily_leads group by make_name) L
on S.make_name=L.make_name
outer join
(select partner_name from partners) P
on L.partner_id=P.partnerid
b. Write a query to Aggregate data to get daily leads and sales count by make and date
Attempt:
select count(sale_id), count(lead_id)
from daily_leads dl
full outer join
on daily_sales ds
on dl.lead_id=ds.lead_id
group by make_name, date
TABLES:
Daily_Leads
+---------------------------------------------------+
| lead_id partner_id date_id make_name |
+---------------------------------------------------+
| 4 2 2019-01-01 toyota |
| 6 2 2019-01-01 honda |
| 8 2 2019-01-02 toyota |
| 9 70 2019-01-01 honda |
| 2 70 2019-01-01 ford |
| 7 1 2019-01-01 bmw |
| 10 4 2019-01-01 bmw |
| 25 1 2019-01-02 ford |
| |
+---------------------------------------------------+
Table Name: daily_sales
+-----------------------------------------------------------------+
| lead_id partner_id date_id make_name Sale_id |
+-----------------------------------------------------------------+
| 4 2 2019-01-01 toyota 1 |
| 6 2 2019-01-01 honda 2 |
| 8 2 2019-01-02 toyota 3 |
| 9 70 2019-01-02 honda 4 |
| 10 4 2019-01-01 bmw 5 |
+-----------------------------------------------------------------+
Table name partners
+----------------------+
| id partner_name |
+----------------------+
| 1 True |
| 2 App |
| 4 Way |
| 70 Fbook |
+----------------------+
I am trying outer joins which I know is probably wrong. Can someone please help me out?
I am new to StackOverFlow community, so I might not have a proper approach to asking questions. I am learning. Would be great if someone can answer these questions.
I guess that you can use the query joining two aggregate queries.
For a., the query is below:
select
M.make_name,
P.partner_name,
L.totalleads,
S.totalsales,
S.totalsales/L.totalleads as close_rate
from
(select distinct make_name, partner_id from daily_leads
union select distinct make_name, partner_id from daily_sales) M
left outer join
(select make_name, partner_id, count(lead_id) as totalleads
from daily_leads
group by make_name, partner_id) L
on M.make_name=L.make_name and M.partner_id=L.partner_id
left outer join
(select make_name, partner_id, count(sale_id) as totalsales
from daily_sales
group by make_name, partner_id) S
on M.make_name=S.make_name and M.partner_id=S.partner_id
left outer join partners P
on M.partner_id=P.id;
db fiddle
For b., it's similar to a query that changed partner_id to date_id of the a. query.

How to print the students name in this query?

The concerned tables are as follows:
students(rollno, name, deptcode)
depts(deptcode, deptname)
course(crs_rollno, crs_name, marks)
The query is
Find the name and roll number of the students from each department who obtained
highest total marks in their own department.
Consider:
i) Courses of different department are different.
ii) All students of a particular department take same number and same courses.
Then only the query makes sense.
I wrote a successful query for displaying the maximum total marks by a student in each department.
select do.deptname, max(x.marks) from students so
inner join depts do
on do.deptcode=so.deptcode
inner join(
select s.name as name, d.deptname as deptname, sum(c.marks) as marks from students s
inner join crs_regd c
on s.rollno=c.crs_rollno
inner join depts d
on d.deptcode=s.deptcode
group by s.name,d.deptname) x
on x.name=so.name and x.deptname=do.deptname group by do.deptname;
But as mentioned I need to display the name as well. Accordingly if I include so.name in select list, I need to include it in group by clause and the output is as below:
Kendra Summers Computer Science 274
Stewart Robbins English 80
Cole Page Computer Science 250
Brian Steele English 83
expected output:
Kendra Summers Computer Science 274
Brian Steele English 83
Where is the problem?
I guess this can be easily achieved if you use window function -
select name, deptname, marks
from (select s.name as name, d.deptname as deptname, sum(c.marks) as marks,
row_number() over(partition by d.deptname order by sum(c.marks) desc) rn
from students s
inner join crs_regd c on s.rollno=c.crs_rollno
inner join depts d on d.deptcode=s.deptcode
group by s.name,d.deptname) x
where rn = 1;
To solve the problem with a readable query I had to define a couple of views:
total_marks: For each student the sum of their marks
create view total_marks as select s.deptcode, s.name, s.rollno, sum(c.marks) as total from course c, students s where s.rollno = c.crs_rollno group by s.rollno;
dept_max: For each department the highest total score by a single student of that department
create view dept_max as select deptcode, max(total) max_total from total_marks group by deptcode;
So I can get the desidered output with the query
select a.deptcode, a.rollno, a.name from total_marks a join dept_max b on a.deptcode = b.deptcode and a.total = b.max_total
If you don't want to use views you can replace their selects on the final query, which will result in this:
select a.deptcode, a.rollno, a.name
from
(select s.deptcode, s.name, s.rollno, sum(c.marks) as total from course c, students s where s.rollno = c.crs_rollno group by s.rollno) a
join (select deptcode, max(total) max_total from (select s.deptcode, s.name, s.rollno, sum(c.marks) as total from course c, students s where s.rollno = c.crs_rollno group by s.rollno) a_ group by deptcode) b
on a.deptcode = b.deptcode and a.total = b.max_total
Which I'm sure it is easily improvable in performance by someone more skilled then me...
If you (and anybody else) want to try it the way I did, here is the schema:
create table depts ( deptcode int primary key auto_increment, deptname varchar(20) );
create table students ( rollno int primary key auto_increment, name varchar(20) not null, deptcode int, foreign key (deptcode) references depts(deptcode) );
create table course ( crs_rollno int, crs_name varchar(20), marks int, foreign key (crs_rollno) references students(rollno) );
And here all the entries I inserted:
insert into depts (deptname) values ("Computer Science"),("Biology"),("Fine Arts");
insert into students (name,deptcode) values ("Turing",1),("Jobs",1),("Tanenbaum",1),("Darwin",2),("Mendel",2),("Bernard",2),("Picasso",3),("Monet",3),("Van Gogh",3);
insert into course (crs_rollno,crs_name,marks) values
(1,"Algorithms",25),(1,"Database",28),(1,"Programming",29),(1,"Calculus",30),
(2,"Algorithms",24),(2,"Database",22),(2,"Programming",28),(2,"Calculus",19),
(3,"Algorithms",21),(3,"Database",27),(3,"Programming",23),(3,"Calculus",26),
(4,"Zoology",22),(4,"Botanics",28),(4,"Chemistry",30),(4,"Anatomy",25),(4,"Pharmacology",27),
(5,"Zoology",29),(5,"Botanics",27),(5,"Chemistry",26),(5,"Anatomy",25),(5,"Pharmacology",24),
(6,"Zoology",18),(6,"Botanics",19),(6,"Chemistry",22),(6,"Anatomy",23),(6,"Pharmacology",24),
(7,"Sculpture",26),(7,"History",25),(7,"Painting",30),
(8,"Sculpture",29),(8,"History",24),(8,"Painting",30),
(9,"Sculpture",21),(9,"History",19),(9,"Painting",25) ;
Those inserts will load these data:
select * from depts;
+----------+------------------+
| deptcode | deptname |
+----------+------------------+
| 1 | Computer Science |
| 2 | Biology |
| 3 | Fine Arts |
+----------+------------------+
select * from students;
+--------+-----------+----------+
| rollno | name | deptcode |
+--------+-----------+----------+
| 1 | Turing | 1 |
| 2 | Jobs | 1 |
| 3 | Tanenbaum | 1 |
| 4 | Darwin | 2 |
| 5 | Mendel | 2 |
| 6 | Bernard | 2 |
| 7 | Picasso | 3 |
| 8 | Monet | 3 |
| 9 | Van Gogh | 3 |
+--------+-----------+----------+
select * from course;
+------------+--------------+-------+
| crs_rollno | crs_name | marks |
+------------+--------------+-------+
| 1 | Algorithms | 25 |
| 1 | Database | 28 |
| 1 | Programming | 29 |
| 1 | Calculus | 30 |
| 2 | Algorithms | 24 |
| 2 | Database | 22 |
| 2 | Programming | 28 |
| 2 | Calculus | 19 |
| 3 | Algorithms | 21 |
| 3 | Database | 27 |
| 3 | Programming | 23 |
| 3 | Calculus | 26 |
| 4 | Zoology | 22 |
| 4 | Botanics | 28 |
| 4 | Chemistry | 30 |
| 4 | Anatomy | 25 |
| 4 | Pharmacology | 27 |
| 5 | Zoology | 29 |
| 5 | Botanics | 27 |
| 5 | Chemistry | 26 |
| 5 | Anatomy | 25 |
| 5 | Pharmacology | 24 |
| 6 | Zoology | 18 |
| 6 | Botanics | 19 |
| 6 | Chemistry | 22 |
| 6 | Anatomy | 23 |
| 6 | Pharmacology | 24 |
| 7 | Sculpture | 26 |
| 7 | History | 25 |
| 7 | Painting | 30 |
| 8 | Sculpture | 29 |
| 8 | History | 24 |
| 8 | Painting | 30 |
| 9 | Sculpture | 21 |
| 9 | History | 19 |
| 9 | Painting | 25 |
+------------+--------------+-------+
I take chance to point out that this database is badly designed. This becomes evident with course table. For these reasons:
The name is singular
This table does not represent courses, but rather exams or scores
crs_name should be a foreign key referencing the primary key of another table (that would actually represent the courses)
There is no constrains to limit the marks to a range and to avoid a student to take twice the same exam
I find more logical to associate courses to departments, instead of student to departments (this way also would make these queries easier)
I tell you this because I understood you are learning from a book, so unless the book at one point says "this database is poorly designed", do not take this exercise as example to design your own!
Anyway, if you manually resolve the query with my data you will come to this results:
+----------+--------+---------+
| deptcode | rollno | name |
+----------+--------+---------+
| 1 | 1 | Turing |
| 2 | 6 | Bernard |
| 3 | 8 | Monet |
+----------+--------+---------+
As further reference, here the contents of the views I needed to define:
select * from total_marks;
+----------+-----------+--------+-------+
| deptcode | name | rollno | total |
+----------+-----------+--------+-------+
| 1 | Turing | 1 | 112 |
| 1 | Jobs | 2 | 93 |
| 1 | Tanenbaum | 3 | 97 |
| 2 | Darwin | 4 | 132 |
| 2 | Mendel | 5 | 131 |
| 2 | Bernard | 6 | 136 |
| 3 | Picasso | 7 | 81 |
| 3 | Monet | 8 | 83 |
| 3 | Van Gogh | 9 | 65 |
+----------+-----------+--------+-------+
select * from dept_max;
+----------+-----------+
| deptcode | max_total |
+----------+-----------+
| 1 | 112 |
| 2 | 136 |
| 3 | 83 |
+----------+-----------+
Hope I helped!
Try the following query
select a.name, b.deptname,c.marks
from students a
, crs_regd b
, depts c
where a.rollno = b.crs_rollno
and a.deptcode = c.deptcode
and(c.deptname,b.marks) in (select do.deptname, max(x.marks)
from students so
inner join depts do
on do.deptcode=so.deptcode
inner join (select s.name as name
, d.deptname as deptname
, sum(c.marks) as marks
from students s
inner join crs_regd c
on s.rollno=c.crs_rollno
inner join depts d
on d.deptcode=s.deptcode
group by s.name,d.deptname) x
on x.name=so.name
and x.deptname=do.deptname
group by do.deptname
)
Inner/Sub query will fetch the course name and max marks and the outer query gets the corresponding name of the student.
try and let know if you got the desired result
Dense_Rank() function would be helpful in this scenario:
SELECT subquery.*
FROM (SELECT Student_Total_Marks.rollno,
Student_Total_Marks.name,
Student_Total_Marks.deptcode, depts.deptname,
rank() over (partition by deptcode order by total_marks desc) Student_Rank
FROM (SELECT Stud.rollno,
Stud.name,
Stud.deptcode,
sum(course.marks) total_marks
FROM students stud inner join course course on stud.rollno = course.crs_rollno
GROUP BY stud.rollno,Stud.name,Stud.deptcode) Student_Total_Marks,
dept dept
WHERE Student_Total_Marks.deptcode = dept.deptname
GROUP BY Student_Total_Marks.deptcode) subquery
WHERE suquery.student_rank = 1

how to sql query 2 tables and group by?

i have 2 tables: activities and users.
users has columns: name, active
activities: name, type, time, user_id.
for example i have these tables:
users
-----
id | name | active
1 | marc | true
2 | john | true
3 | mary | true
4 | nico | true
activities
-----
id | name | type | time | user_id
1 | morn | walk | 90 | 2
2 | morn | walk | 22 | 2
3 | morn | run | 12 | 2
4 | sat | walk | 22 | 1
5 | morn | run | 13 | 1
6 | mond | walk | 22 | 3
7 | morn | walk | 22 | 2
8 | even | run | 42 | 1
9 | morn | walk | 22 | 3
10 | morn | walk | 62 | 1
11 | morn | run | 22 | 3
now i would like to get table that would sum time spent on each type of activity and would group it by user name. so:
result
------
user name | type | time
marc | walk | 84
marc | run | 55
john | walk | 134
john | run | 12
mary | walk | 44
mary | run | 2
nico | walk | 0
nico | run | 0
how should i write this query to get this result?
thanks in advance
gerard
you can use coalesce to get 0 for empty activities and distinct to get all type of possible activities
select
u.name, c.type,
coalesce(sum(a.time), 0) as time
from (select distinct type from activities) as c
cross join users as u
left outer join activities as a on a.user_id = u.id and a.type = c.type
group by u.name, c.type
order by u.name, c.type
sql fiddle demo
Select u.name, a.type, SUM(a.time) FROM
activities a
LEFT JOIN users u
ON a.user_id = u.id
GROUP BY u.name, a.type
FIDDLE
Use this to get zero count as well
SELECT c.name,c.type,aa.time FROM
(Select u.id,u.name, b.type FROM
users u
CROSS JOIN (SELECT DISTINCT type FROM activities) b) c
LEFT JOIN (SELECT a.user_id, a.type, SUM(a.time) as time FROM
activities a
GROUP BY a.user_id, a.type) aa ON
aa.user_id = c.id and c.type = aa.type
Fiddle2
this might work :
select users.name,activities.type,sum(activities.time)
from users left join activities on users.id = activities.user_id
where users.active group by users.name,activities.type

Querying a many to many table

I'm trying to query a table and I'm having a hard time figuring out the query.
This are my tables(simplified):
Member
ID | NAME
1 | Frans
2 | Eric
3 | Stephan
4 | Kris
Evenement
ID | NAME
1 | Picknic
2 | Party
3 | Movie
Evenement
ID_EVENEMENT | ID_MEMBER
1 | Kris
1 | Stephan
1 | Eric
2 | Eric
2 | Frans
3 | Frans
3 | Stephan
Alright, the query I want to do is this:
I want to
select
member_evenement.ID_MEMBER and member_evenement.ID_EVENEMENT
from
member_evenement
where
member.ID on member_evenement.ID_MEMBER
where
member_evenement.ID_MEMBER does not exist
for each member_evenement.ID_EVENEMENT
separately.
I'm using sql server 2008 R2
I hope I explained my question well enough.
If these are my base tables
Member
ID | NAME
1 | Frans
2 | Eric
3 | Stephan
4 | Kris
Evenement
ID | NAME
1 | Picknic
2 | Party
3 | Movie
Member_Evenement
ID_EVENEMENT | ID_MEMBER
1 | Kris
1 | Stephan
1 | Eric
2 | Eric
2 | Frans
3 | Frans
3 | Stephan
then the result of my query should look like this:
Evenement
ID_EVENEMENT | ID_MEMBER | MEMBER_NAME | EVENEMENT_NAME
1 | 1 | Frans | Picknic
2 | 3 | Stephan | Party
2 | 4 | Kris | Party
3 | 2 | Eric | Movie
3 | 4 | Kris | Movie
SELECT e.ID AS ID_EVENEMENT, m.ID AS ID_MEMBER,
FROM Evenement e, Member m
EXCEPT
SELECT ID_EVENEMENT, ID_MEMBER
FROM member_evenement;
To return all combinations of member and evenement that are not recorded on the member_evenement table, try the following:
select e.id id_evenement,
m.id id_member,
m.name member_name,
e.name evenement_name
from member m
cross join evenement e
left join member_evenement me
on e.id = me.id_evenement and m.id = me.id_member
where me.id_evenement is null or me.id_member is null
(This assumes that id_member is actually the member's id, and not their name as in the sample data.)
Another possibility, which gives the exact same execution plan as Mark Bannister's and onedaywhen's answer is the following:
SELECT member.id AS memberid, evenement.id AS evenementid
FROM member
CROSS JOIN evenement WHERE
NOT EXISTS(
SELECT NULL AS [Empty]
FROM member_evenement
WHERE member_evenement.memberid = member.id
AND member_evenement.evenementid = evenement.id
)