Finding applicant that applied to more than 2 jobs - sql
I'm trying to Find the applicant who has applied to more than 2 different companies
The applicants shown should be:
Monica (a2): jobs 101, j02, and j04
Jim (a3): jobs j02, j04, and j06
This is the script used for this!!
drop table Jobskills;
drop table Appskills;
drop table Applies;
drop table Applicant;
drop table Skills;
drop table Job;
drop table Company;
create table Company(compid char(5) primary key, compname varchar(20),
comptype varchar(15));
create table Job(jobid char(5) primary key, jobtitle varchar(20),
salarylow int, salaryhigh int, location char(10),
compid references Company(compid) on delete cascade);
create table Skills(skillid char(5) primary key, skillname varchar(15));
create table Jobskills(jobid references Job(jobid) on delete cascade,
skillid references Skills(skillid),
expertiseneeded int, primary key(jobid,skillid));
create table Applicant(appid char(5) primary key, name varchar(15),
age int, highdegree char(5), expected_salary int) ;
create table AppSkills(appid references Applicant(appid) on delete cascade,
skillid references Skills(skillid), expertise int,
primary key(appid, skillid));
create table Applies(jobid references Job(jobid),
appid references Applicant(appid) on delete cascade,
appdate date, decisiondate date, outcome char(10),
primary key(jobid, appid));
rem Initial Company data
insert into Company values('PWC', 'Price Waterhouse', 'consulting');
insert into Company values('MSFT', 'Microsoft', 'software');
insert into Company values('INTL', 'Intel', 'electronics');
insert into Company values('NCR', 'NCR Corp', 'server');
insert into Company values('WPAF', 'WP Air Force', 'defense');
insert into Company values('DLT', 'Deloitte', 'consulting');
rem Initial Job data
insert into Job values('101', 'Programmer', 55000, 60000, 'Redmond', 'MSFT');
insert into Job values('j02', 'Designer', 42000, 45000, 'Redmond', 'MSFT');
insert into Job values('j03', 'SAP impl', 30000, 40000, 'Chicago', 'PWC');
insert into Job values('j04', 'Proj mgmt', 35000, 55000, 'Chicago', 'PWC');
insert into Job values('j05', 'SOX', 60000, 65000, 'Detroit', 'PWC');
insert into Job values('j06', 'db admin', 45000, 50000, 'Dayton', 'NCR');
insert into Job values('j07', 'db designer', 35000, 40000, 'Dayton', 'NCR');
insert into Job values('j08', 'intern', 25000, 28000, 'Dayton', 'NCR');
insert into Job values('j09', 'engineer', 52000, 55000, 'Dayton','WPAF');
insert into Job values('j10', 'dba', 62000, 65000, 'Dayton','WPAF');
insert into Job values('j11', 'hardware dev', 50000, 65000, 'NYC','INTL');
insert into Job values('j12', 'pcb designer', 55000, 68000,'NYC','INTL');
insert into Job values('j13', 'chip designer', 40000, 55000,'Chicago','INTL');
insert into Job values('j14', 'IT', 40000, 60000, 'Dayton', 'DLT');
insert into Job values('j15', 'IT', 50000, 70000, 'Chicago', 'DLT');
rem initial Skills data
insert into Skills values('s1', 'database');
insert into Skills values('s2', 'programming');
insert into Skills values('s3', 'sox');
insert into Skills values('s4', 'project');
insert into Skills values('s5', 'hardware');
insert into Skills values('s6', 'sap');
insert into Skills values('s7', 'analysis');
rem Initial Jobskills data
insert into Jobskills values('101', 's2', 5);
insert into Jobskills values('101', 's7', 4);
insert into Jobskills values('j02', 's2', 3);
insert into Jobskills values('j02', 's7', 5);
insert into Jobskills values('j03', 's6', 5);
insert into Jobskills values('j04', 's7', 4);
insert into Jobskills values('j04', 's4', 5);
insert into Jobskills values('j04', 's2', 2);
insert into Jobskills values('j05', 's3', 5);
insert into Jobskills values('j06', 's1', 5);
insert into Jobskills values('j06', 's2', 3);
insert into Jobskills values('j07', 's1', 4);
insert into Jobskills values('j07', 's7', 3);
insert into Jobskills values('j08', 's1', 2);
insert into Jobskills values('j09', 's2', 4);
insert into Jobskills values('j09', 's4', 4);
insert into Jobskills values('j10', 's4', 3);
insert into Jobskills values('j10', 's1', 5);
insert into Jobskills values('j11', 's5', 3);
insert into Jobskills values('j11', 's4', 3);
insert into Jobskills values('j12', 's5', 5);
insert into Jobskills values('j13', 's1', 4);
insert into Jobskills values('j13', 's2', 5);
insert into Jobskills values('j14', 's7', 4);
rem initial Applicants data
insert into Applicant values('a1', 'Joe', 30, 'MS', 55000);
insert into Applicant values('a2', 'Monica', 25, 'BS', 62000);
insert into Applicant values('a3', 'Jim', 22, 'BS', 45000);
insert into Applicant values('a4', 'Monica', 25, 'BS', 34000);
rem initial Appskills data
insert into Appskills values('a1', 's1', 3);
insert into Appskills values('a1', 's2', 4);
insert into Appskills values('a1', 's4', 4);
insert into Appskills values('a1', 's6', 3);
insert into Appskills values('a1', 's7', 4);
insert into Appskills values('a2', 's2', 3);
insert into Appskills values('a2', 's3', 5);
insert into Appskills values('a2', 's6', 4);
insert into Appskills values('a3', 's4', 3);
insert into Appskills values('a3', 's1', 3);
insert into Appskills values('a3', 's2', 5);
rem Applies
insert into Applies values ('101', 'a1', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('101', 'a2', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('j02', 'a2', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('j04', 'a2', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('j02', 'a3', '01-JAN-06', '08-JAN-06', 'nohire');
insert into Applies values ('j04', 'a3', '01-JAN-06', '08-JAN-06', 'nohire');
insert into Applies values ('j06', 'a3', '01-JAN-06', '08-JAN-06', 'nohire');
This is my code so far...
SELECT app.name, app.appid, j.jobid, c.compname
FROM applicant app INNER JOIN applies apps ON app.appid = apps.appid INNER JOIN
job j ON j.jobid = apps.jobid INNER JOIN company c ON c.compid = j.compid
WHERE apps.appid > '2'
Something is wrong though because I get Joe (a1) and I should not be getting him. I should only be getting Monica (a2) and Jim (a3). What am I missing?
Hmmm . . . You need aggregation. To answer your question, you only need a HAVING clause and the job and companies are not important. But you can put them into delimited strings if you like:
SELECT app.name, app.appid,
LIST_AGG(j.jobid, ',') WITHIN GROUP (ORDER BY j.jobid),
LIST_AGG(c.compname, ',') WITHIN GROUP (ORDER BY c.compname)
FROM applicant app INNER JOIN
applies apps
ON app.appid = apps.appid INNER JOIN
job j
ON j.jobid = apps.jobid INNER JOIN
company c
ON c.compid = j.compid
GROUP BY app.name, app.id,
HAVING COUNT(DISTINCT c.compname) > 2;
Here in common table expression I have ranked the unique university names for an applicant . Then with exists I have counted only applicants with more than one university count and showed information for those applicants only.
with cte as (
SELECT app.name, app.appid, j.jobid, c.compname, dense_rank()over(partition by app.appid order by compname)companycount
FROM applicant app INNER JOIN applies apps ON app.appid = apps.appid INNER JOIN
job j ON j.jobid = apps.jobid INNER JOIN company c ON c.compid = j.compid)
select name,appid,jobid,compname from cte c
where exists (select appid from cte ct where ct.companycount>1 and c.appid=ct.appid)
Related
To find passengers travelling more than thrice on the same route in PL/SQL
Here I am creating three tables, one for storing the Train Info. Another for holding onto the Passenger info and the other one to hold the ticket Info. create table T_Train_Info( route_no integer primary key, source varchar2(50), destination varchar2(50) ) create table T_Pay_Info( pax_id integer primary key, pax_name varchar2(50), dob date, gender varchar2(5) ) create table T_Tkt_Info( pax_id integer, route_no integer, journey_date date, seat_no varchar2(5), primary key(pax_id, route_no, journey_date) ) In the Train_Info table, I am inserting two unique routes with the same source and destination as there can be different routes for the same source and destination. And filling the other tables in the same manner. In the ticket table, I am repeating values because I aim to find the passenger travelling thrice on the same route. insert into T_Train_Info values(1, 'Chennai', 'Pune'); insert into T_Train_Info values(2, 'Chennai', 'Pune'); insert into T_Train_Info values(3, 'Bangalore', 'Kolkata'); insert into T_Tkt_Info values(100, 1, to_date('11/03/2022', 'DD/MM/YYYY'), 22); insert into T_Tkt_Info values(100, 1, to_date('14/08/2022', 'DD/MM/YYYY'), 23); insert into T_Tkt_Info values(100, 1, to_date('29/08/2022', 'DD/MM/YYYY'), 24); insert into T_Tkt_Info values(102, 3, to_date('22/08/2022', 'DD/MM/YYYY'), 24); insert into T_Tkt_Info values(100, 1, to_date('27/08/2022', 'DD/MM/YYYY'), 24); insert into T_Tkt_Info values(100, 2, to_date('28/08/2022', 'DD/MM/YYYY'), 24); insert into T_Pay_Info values(100, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F'); insert into T_Pay_Info values(101, 'B', to_date('23/09/2023', 'DD/MM/YYYY'), 'M'); insert into T_Pay_Info values(102, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F'); insert into T_Pay_Info values(103, 'D', to_date('23/09/2023', 'DD/MM/YYYY'), 'M'); insert into T_Pay_Info values(104, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F'); insert into T_Pay_Info values(105, 'A', to_date('23/09/2023', 'DD/MM/YYYY'), 'M'); Here's my procedure which keeps returning the error saying 'exact fetch returns more than requested number of rows' at the select statement. What am I doing wrong here? create or replace procedure pr_pass_route_details(x in T_Train_Info.Source%type, y in T_Train_Info.Destination%type) is pr_name T_Pay_Info.Pax_Name%type; begin for i in (select pax_id from t_tkt_info group by pax_id,route_no having count(*) >=3) loop select pax_name into pr_name from t_pay_info where pax_id = i.pax_id and T_Train_Info.Source=x and T_Train_Info.Destination=y; dbms_output.put_line(pr_name); end loop; end pr_pass_route_details;
i’m not sure why you’ve written a SP to do this as you can achieve this with a simple query: SELECT pax_id, route_no, COUNT(journey_date) FROM T_Tkt_Info GROUP BY pax_id, route_no HAVING COUNT(journey_date) = 3
SQL Query: To list the majors with more than 2 students in it
I am trying to figure out a query that will list the major that has more than 2 students in it (CS) but I am not sure how to go about it. This is the script to create the table for SQL! REM drop all the tables. Note that you need to drop the REM dependent table first before dropping the base tables. drop table Reg; drop table Student; drop table Course; REM Now create all the tables. create table Student ( sid char(10) primary key, sname varchar(20) not null, gpa float, major char(10), dob DATE ); create table Course ( cno char(10) primary key, cname varchar(20) not null, credits int, dept char(10) ); create table Reg ( sid references Student(sid) on delete cascade, cno references Course(cno) on delete cascade, grade char(2), primary key (sid, cno) ); REM Now insert all the rows. insert into Student values ('111', 'Joe', 3.5 , 'MIS', '01-AUG-2000'); insert into Student values ('222', 'Jack', 3.4 , 'MIS', '12-JAN-1999'); insert into Student values ('333', 'Jill', 3.2 , 'CS', '15-MAY-1998'); insert into Student values ('444', 'Mary', 3.7 , 'CS', '17-DEC-2001'); insert into Student values ('555', 'Peter', 3.8 , 'CS', '19-MAR-1999'); insert into Student values ('666', 'Pat', 3.9, 'Math', '31-MAY-2000'); insert into Student values ('777', 'Tracy', 4.0, 'Math', '18-JUL-1997'); insert into Course values ('c101', 'intro', 3 , 'CS'); insert into Course values ('m415', 'database', 4 , 'Bus'); insert into Course values ('m215', 'programming', 4 , 'Bus'); insert into Course values ('a444', 'calculus', 3 , 'Math'); insert into Reg values ('111', 'c101', 'A'); insert into Reg values ('111', 'm215', 'B'); insert into Reg values ('111', 'm415', 'A'); insert into Reg values ('222', 'm215', 'A'); insert into Reg values ('222', 'm415', 'B'); insert into Reg values ('333', 'c101', 'A'); insert into Reg values ('444', 'm215', 'C'); insert into Reg values ('444', 'm415', 'B'); insert into Reg values ('555', 'c101', 'B'); insert into Reg values ('555', 'm215', 'A'); insert into Reg values ('555', 'm415', 'A'); insert into Reg values ('666', 'c101', 'A'); Thank you so much!
this will list group by major within CS and having more than 2 select major ,count(1) from Student where major ='CS' group by major having count(1)>2
Can I reference this table?
I am trying to show amount paid for each tutor sorted by month and then by tutor id. I have the first part correct and can sort by month but cannot sort by tutor id because it is from a different table. Here is the script for my tables: create table match_history (match_id number(3), tutor_id number(3), student_id number(4), start_date date, end_date date, constraint pk_match_history primary key (match_id), constraint fk1_match_history foreign key (tutor_id) references tutor(tutor_id), constraint fk2_match_history foreign key (student_id) references student(student_id)); create table tutor_report (match_id number(3), month date, hours number(3), lessons number(3), constraint pk_tutor_report primary key (match_id, month), constraint fk1_tutor_report foreign key (match_id) references match_history(match_id)); insert into tutor values (100, '05-JAN-2017', 'Active'); insert into tutor values (101, '05-JAN-2017', 'Temp Stop'); insert into tutor values (102, '05-JAN-2017', 'Dropped'); insert into tutor values (103, '22-MAY-2017', 'Active'); insert into tutor values (104, '22-MAY-2017', 'Active'); insert into tutor values (105, '22-MAY-2017', 'Temp Stop'); insert into tutor values (106, '22-MAY-2017', 'Active'); insert into student values (3000, 2.3); insert into student values (3001, 5.6); insert into student values (3002, 1.3); insert into student values (3003, 3.3); insert into student values (3004, 2.7); insert into student values (3005, 4.8); insert into student values (3006, 7.8); insert into student values (3007, 1.5); insert into match_history values (1, 100, 3000, '10-JAN-2017', null); insert into match_history values (2, 101, 3001, '15-JAN-2017', '15-MAY-2017'); insert into match_history values (3, 102, 3002, '10-FEB-2017', '01-MAR-2017'); insert into match_history values (4, 106, 3003, '28-MAY-2017', null); insert into match_history values (5, 103, 3004, '01-JUN-2017', '15-JUN-2017'); insert into match_history values (6, 104, 3005, '01-JUN-2017', '28-JUN-2017'); insert into match_history values (7, 104, 3006, '01-JUN-2017', null); insert into tutor_report values (1, '01-JUN-2017', 8, 4); insert into tutor_report values (4, '01-JUN-2017', 8, 6); insert into tutor_report values (5, '01-JUN-2017', 4, 4); insert into tutor_report values (4, '01-JUL-2017', 10, 5); insert into tutor_report values (1, '01-JUL-2017', 4, 2); This is what I have so far: Select (hours * 10) as amount paid from tutor_report group by month, tutor_id however obviously I cannot just say tutor_id at the end.
You can join match_history to get the tutor_id. But your statement and the query don't match. If you want to sort use ORDER BY. SELECT tr.hours * 10 amount_paid FROM tutor_report tr INNER JOIN match_history mh ON mh.match_id = tr.match_id ORDER BY tr.month, mh.tutor_id; If you want to aggregate, hours needs to be argument to some aggregation function. Maybe you're after the sum of hours? SELECT sum(tr.hours) * 10 amount_paid FROM tutor_report tr INNER JOIN match_history mh ON mh.match_id = tr.match_id GROUP BY tr.month, mh.tutor_id;
If you are grouping based on columns on two tables,you need to join them on the matching Id and then use group by Select (hours * 10) as amount paid from tutor_report a join match_history b on a. match_id = b.match_id group by month, tutor_id
SQL Query count
HI there I have this table, Recipe = (idR, recipeTitle, prepText, cuisineType, mealType) Ingredient = (idI, ingrDesc) RecipIngr = (idR*, idI*) and I'm trying to query a list for ingrDesc with a count of how many recipies that ingrDesc is in. I want to list only those ingrDesc that occur more than 10 times. Here's what I have: SELECT a.idI, a.recipeTitle FROM Recipe a INNER JOIN recpingr b ON a.idr = b.idr WHERE a.preptext = '>10' Any help as I don't know how to carry on with this query
Use GROUP BY with HAVING: SELECT i.idI, i.ingrDesc, COUNT(*) FROM Ingredient i INNER JOIN RecipIngr ri ON i.idI = ri.idI GROUP BY i.idI, i.ingrDesc HAVING COUNT(*) > 10
You need to use a group by clause and having. I have created a quick sample here but my sample data does not go up to 10 so I used any ingredient that was used more than once (> 1). Here is the sample data: create table dbo.recipe ( idR int not null, recipeTitle varchar(100) not null, prepText varchar(4000) null, cuisineType varchar(100) null, mealType varchar(100) null ) go insert into dbo.recipe values (1, 'Eggs and Bacon', 'Prep Text 1', 'American', 'Breakfast') insert into dbo.recipe values (2, 'Turkey Sandwich', 'Prep Text 2', 'American', 'Lunch') insert into dbo.recipe values (3, 'Roast Beef Sandwich', 'Prep Text 3', 'American', 'Lunch') go create table dbo.ingredient ( idI int not null, ingrDesc varchar(200) not null ) go insert into dbo.ingredient values (1, 'Large Egg') insert into dbo.ingredient values (2, 'Bacon'); insert into dbo.ingredient values (3, 'Butter'); insert into dbo.ingredient values (4, 'Sliced Turkey'); insert into dbo.ingredient values (5, 'Lettuce'); insert into dbo.ingredient values (6, 'Tomato'); insert into dbo.ingredient values (7, 'Onion'); insert into dbo.ingredient values (8, 'Bread'); insert into dbo.ingredient values (9, 'Mustard'); insert into dbo.ingredient values (10, 'Horseradish'); insert into dbo.ingredient values (11, 'Sliced Roast Beef'); go create table dbo.recipingr( idR int not null, idI int not null ) go insert into dbo.recipingr values (1, 1); insert into dbo.recipingr values (1, 2); insert into dbo.recipingr values (2, 4); insert into dbo.recipingr values (2, 5); insert into dbo.recipingr values (2, 6); insert into dbo.recipingr values (2, 7); insert into dbo.recipingr values (2, 8); insert into dbo.recipingr values (2, 9); insert into dbo.recipingr values (3, 11); insert into dbo.recipingr values (3, 10); insert into dbo.recipingr values (3, 8); insert into dbo.recipingr values (3, 6); insert into dbo.recipingr values (3, 5); go Here is the query: select i.ingrDesc, count(*) ingrCount from dbo.recipe r inner join dbo.recipingr ri on ri.idR = r.idR inner join dbo.ingredient i on i.idI = ri.idI group by i.ingrDesc having count(*) > 1
convert marks into percentage
how to convert marks obtained by a student into x% i.e. there are two exams. calculate certain %marks from both exams (say x% and Y%) so that the total will be 100%
Based on the limited info that you have provided, I think you might be asking for the following: create table student ( id int, s_name varchar(10) ) insert into student values (1, 'Jim') insert into student values (2, 'Bob') insert into student values (3, 'Jane') create table exams ( id int, e_name varchar(10) ) insert into exams values (1, 'Test 1') insert into exams values (2, 'Test 2') insert into exams values (3, 'Test 3') insert into exams values (4, 'Test 4') create table exam_student ( e_id int, s_id int, dt datetime, score decimal(5,2) ) insert into exam_student values(1, 1, '2012-08-01', 65.0) insert into exam_student values(1, 2, '2012-08-01', 85.0) insert into exam_student values(2, 1, '2012-08-02', 75.0) insert into exam_student values(2, 2, '2012-08-02', 42.0) select avg(es.score) as ScorePct, s_id, s.s_name from exam_student es inner join exams e on es.e_id = e.id inner join student s on es.s_id = s.id group by s_id, s_name Results: If you provide more details on exactly what you are looking for that would be helpful in answering your question.