I was reading the question which asks to
Q - "Find all the instructors who have higher salary than some instructor in 'Comp Sci'."
Relation given --- instructor(Id, name, salary, dept_name)
Answer given was -
select distinct T.name
from instructor T, instructor S
where T.salary> S.salary and S.dept_name = 'Comp Sci'
Thanks in advance. Appreciate your time.
My thought was to write it in nested select query as in like -
select name
from instructor
where salary > (select min(salary)
from instructor
where dept_name = 'Comp Sci')
Can anyone explain which is one efficient and why?
Related
I have three (3) tables: Course, Student, Registration. The columns in each table are:
Course: CourseNo, Title, Department, NumberOfCredits, CourseFees
Student: SID, Name, Address, Telephone, Age
Registration: SID, CourseNo, startDate, CompleteDate, Grade
I'm having a hard time figuring out how to write the correct statement that will list the course numbers and titles of courses that have more than 10 students getting a grade lower than 50. I'd like to be able to GROUP BY CourseNo and have a COUNT based on SID.
Any help would be much appreciated, especially if any explanation can be given.
Thanks in advance!
I hope this help
select c.CourseNo, c.Title, count(SID) AS sCount
from Registration r
join Course c on (c.CourseNo = r.CourseNo)
where r.Grade < 50
group by r.CourseNo
HAVING sCount > 10;
I am doing SQL practice on Leetcode website and a common error I come across is
'test table xxx doesn't exist'
I saw someone was mentioning that I don't have permission to these tables, but I don't understand why. Under what circumstances do I loss permission to the test tables? For example, what specific error in the code can cause this problem? Thanks!
Addition: For example, I was trying to solve the 184. Department Highest Salary leetcode problem with the following code:
select d.Name as "Department", e.Name as "Employee", Salary
from Empolyee e
join Department d
on d.Id = e.DepartmentId
where (e.DepartmentId , Salary) in (select DepartmentId, max(Salary)
from Employee
group by DepartmentId)
The error I receive is:
Table 'test.empolyee' doesn't exist
However, the following code (which is very similar to my above code) is correct! Anyone know why? Thanks!
SELECT Department.name AS 'Department',
Employee.name AS 'Employee',
Salary
FROM Employee
JOIN Department
ON Employee.DepartmentId = Department.Id
WHERE (Employee.DepartmentId , Salary) IN (SELECT DepartmentId, MAX(Salary)
FROM Employee
GROUP BY DepartmentId)
I'm having some doubts regarding these answers of mine, would greatly appreciate if you guys can clarify if im right or wrong,
Questions:
An employee may be assigned to more than one project and a project may have many employees. Consider the following relational schema and write SQL statements for the below queries.
Employees (empID, empName, empDOB, empAddress, salary, deptID, jobID)
Assignments (empID, projID, assignedDate, completionDate, status)
Projects (projID, projDescription, startDate, endDate, projType)
(a) Display the names of employees who were born before 31st Jan 1980 and assigned a ‘Office Complex’ type project, sort results in ascending order of name. (5 marks)
(b) Retrieve the empIDs who are assigned at least two (2) projects. (5 marks)
Answers:
(a) SELECT empName FROM Employees WHERE empDOB < '31-01-1980' AND projType = (SELECT projType FROM Projects WHERE projType = 'Office Complex') ORDER BY empName;
(b) SELECT empID FROM Employees GROUP BY (SELECT projID From Projects) HAVING COUNT(*)>1 ORDER BY empID;
I feel the answer for the second question may be wrong.
For part a), I'd join the tables to match up employees with the project type:
SELECT empName
FROM Employees
INNER JOIN Assignments ON Assignments.empID = Employees.empID
INNER JOIN Projects ON Assignments.projID = Projects.projID
WHERE empDOB < 31-01-1980 AND projType = 'Office Complex'
ORDER BY empName;
As it stands, your statement attempts to find projType = 'Office Complex' in the Employee table, where it doesn't exist.
For the second question, everything you need is in the Assignments table:
SELECT empID, COUNT(projID)
FROM Assignments
GROUP BY empID
HAVING COUNT(projID) > 1
I have the following data:
http://i.imgur.com/e4d8M8V.png?1
The first table is labeled "Offering" and the second is labeled "Instructor."
I am trying to sum the salaries of all the professors who do not have their instructor ID appear in the offering table.
What I did first was generate a table that has the data I need in it:
select distinct i.InstructorID, i.Salary
from Instructor i
where i.InstructorID NOT IN (select o.InstructorID from Offering o);
Which gives me the desired result here:
http://i.imgur.com/rkFKseX.png?1
I then want to add these two salaries and have the result displayed in a single salary column. I've tried code like:
$MySQL:> select sum(i.Salary)
from Instructor i
where i.Salary in ( select distinct i.InstructorID, i.Salary
from Instructor i
where i.InstructorID NOT IN (select o.InstructorID from Offering o));
And get "SQLException: java.sql.SQLException: Operand should contain 1 co." However, I am not sure how to contain the result from my previous query to one column. The way I see it is if I was going to sum all of the salaries that were on the list and I just did a distinct Salary, it would only return three salaries rather than eight salaries, so it would be wrong to do that for my case too.
How would I add the two columns I have generated, and is there an easier way to complete my goal than the method I am using?
You can use subqueries. They are described by this article
You should be able to just wrap this in a sub query:
select SUM(Salary)
from (
select distinct i.InstructorID, i.Salary
from Instructor i
where i.InstructorID NOT IN (select o.InstructorID from Offering o)
)
This will work in SQL Server, I think it will work in MYSQL.
You could probably join the two table and then do a group By.
Something like
select sum(salary) from a, b where a.instructorid = b.instructorid and .....
I think this is what you're trying to do :
$MySQL:> select sum(i.Salary)
from Instructor i
where i.Salary in ( select distinct i.Salary
from Instructor i
where i.InstructorID NOT IN (select o.InstructorID from Offering o));
I bet InstructorID is a primary key in Instructor table and I think you don't need "distinct" in select distinct InstructorID, Salary from Instructor.
select sum(Salary)
from Instructor
where InstructorID not in (select InstructorID from Offering);
Please test 3 different approaches and choose the best.
NOT IN
SELECT SUM(i.Salary)
FROM Instructor i
WHERE i.InstructorID NOT IN (select o.InstructorID from Offering o);
LEFT JOIN with IS NULL
SELECT SUM(i.Salary)
FROM Instructor i LEFT JOIN Offering o USING(InstructorID)
WHERE o.InstructorID IS NULL;
NOT EXISTS
SELECT SUM(i.Salary)
FROM Instructor i
WHERE NOT EXISTS (SELECT 1 FROM Offering o WHERE o.InstructorID = i.InstructorID);
I am trying write a query that will display if the person has anyone with a lower job position
SELECT Count(job)
FROM emp
GROUP BY job
HAVING job < 'MANAGER';
Would anyone be kind enough to help me with this?
Cheers
-Jay
Answer to my question should be:
Number of subORD
---------------------
6
How about:
SELECT COUNT(*) FROM (
SELECT Super
FROM Emp
GROUP BY Super
HAVING Super IS NOT NULL)
What about
Select count(*) as Supers
FROM (
Select distinct e1.EmpNo
from Emp e1 join Emp e2 on e1.EmpNo = e2.Super)
Assuming that you can determine if employee A has subordinates by checking if any other employees include his ID as their "SUPER".
SELECT COUNT(*) FROM (
SELECT Super
FROM Emp
WHERE Super > 0
GROUP BY Super) AS Supervisors