This works to return a name:
SELECT Fname, Lname FROM EMPLOYEE WHERE dno = 4 AND Ssn = 999887777;
returns the employee's name.
But this returns nothing:
SELECT Fname, Lname FROM EMPLOYEE WHERE dno = 4 AND Ssn = (SELECT essn FROM WORKS_ON);
I want to get the names of all employees in Dept. No. 4 whose employee SSNs are found in the WORKS_ON table.
'essn' and 'Ssn' are both 9-character text fields that work in other contexts.
You can't use = to compare a value to a subquery that returns multiple values.
The way to figure out if Ssn is in the result of the subquery is to use IN:
SELECT Fname, Lname
FROM EMPLOYEE
WHERE dno = 4
AND Ssn IN (SELECT essn FROM WORKS_ON);
Related
DB Fiddle here
SELECT Fname,Lname
FROM EMPLOYEE
WHERE Bdate=(MaxBdate)
GROUP BY Fname,Lname
I tried it this way but does not work.
From your fiddle, assuming that supervisors have NULL in column Super_ssn, I think this will work:
SELECT FName, Lname
FROM EMPLOYEE
WHERE
Super_ssn IS NOT NULL
AND
Bdate = (SELECT MIN(Bdate) FROM EMPLOYEE
WHERE Super_ssn IS NOT NULL)
I would assume an employee (non-supervisor) is one whose ssn is not in the supervisor ssn column.
The oldest can be derived from the min windowing function.
with employees_only as (
select e.*, min (bdate) over (partition by 1) as min_bdate
from employee e
where not exists (
select null
from employee s
where s.super_ssn = e.ssn
)
)
select fname, lname
from employees_only
where bdate = min_bdate
Right now I'm working on some beginner SQL stuff and I've run into a problem with a practice question:
List the SSN, name (first and last) and salary of all employees that
are managers. Hint: Use a nested query to obtain the answer.
A manager is someone who has the same SUPERSSN as their SSN. The above question seems straight forward enough to me but when I run my SQL code:
SELECT SSN, FName, LName, Salary
FROM employee
WHERE SSN = SuperSSN
It returns ZERO rows, and I'm not sure why. Any help is greatly appreciated! Thanks.
EDIT!: My mistake. The SuperSSN was varChar whereas the SSN was Int.
EDIT2: The table looks something like this:
SSN BDate Sex Address Salary FName Minit LName DNo SuperSSN
123456789 | 1965-01-09 | M | "731 Fondren, Houston, TX" | 30000 | John B | Smith | 5 | 123456789
If you prefer to keep the SuperSSN column of type VARCHAR, you can try this:
SELECT SSN, FName, LName, Salary
FROM employee
WHERE SSN = (SELECT DISTINCT CAST(SuperSSN AS UNSIGNED) FROM employee WHERE SSN = SSN);
Else, simply this would do it:
SELECT SSN, FName, LName, Salary
FROM employee
WHERE SSN = (SELECT DISTINCT SuperSSN FROM employee WHERE SSN = SSN);
Try the following
SELECT SSN, FName, LName, Salary
FROM employee
WHERE SSN = (SELECT SuperSSN FROM employee WHERE SSN = SSN)
I am trying to generate a SQL query to find out employees who work in multiple department using first name and last name
Are you looking for this query. This will give you the employees working in multiple department.
select
FirstName, LastName, Count(Department)
from
Employee
where
FirstName = '<FirstName>' -- Add condition here
and LastName = '<LastName>'
group by
FirstName, LastName
having
Count(Department) > 1
I am trying to get the fname and lname of the person that makes the most money and the person that makes the least.
I am looking for a solution that uses only one query
Select fname, lname
from Employees
where Salary =51,000
and Salary =$28,000;
I tried to figure this out but I could not.
It is using subqueries and I am having a really hard time figuring it out. It is said to use only 1 query.
select fname, lname, Salary
from Employees
where Salary = (select min(Salary) from Employees) or
Salary = (select max(Salary) from Employees)
EDIT: if you know the values of the top/bottom salary, use this:
Select fname, lname
from Employees
where Salary =51,000
OR
Salary =$28,000;
Emphasis on OR.
I have a table named Employee which has the following columns:
ID(primary key) of type int
Name of type varchar(255)
Designation of type varchar(50)
Salary of type int
I want to write a sub-query, which will give me the names of the employees who have greater salary than ANY employee of the designation 'Junior Officer'.
Here's what I have tried but was unsuccessful:
SELECT Name
FROM Employee
WHERE Salary>
(SELECT Salary FROM Employee WHERE Designation = 'Junior Officer');
Try this.
SELECT Name
FROM Employee
WHERE Salary>
(SELECT max(Salary) FROM Employee WHERE Designation = 'Junior Officer');
Because it is urgent, I'm writing you this without testing it. Try this:
SELECT Name
FROM Employee
WHERE Salary >
(SELECT MAX(Salary) FROM Employee WHERE Designation = 'Junior Officer');