How to check equality in a SQL table rows? - sql

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)

Related

List the oldest employee who is not a supervisor

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

Highest salary per department (also same salary)

How can I select the highest salary on each department with a same salary.
My query is only to get the first row in each department with the same salary. But I want to select all max same salary on each department. Please help me out of this problem.
Below is the sample table:
PSD Department
----------------------
Yumang's Salary: $500
Paus Salary: $500
QA Department
----------------------
Villanueva: $1000
Calacar: $1000
Here's the code I am trying:
SELECT MAX(inter_department_votes.number_votes)
FROM employee_salary
GROUP BY dept_id
try selecting dept_id as well:
SELECT dept_id,
MAX(inter_department_votes.number_votes)
FROM employee_salary GROUP BY dept_id
Using RANK() function:
RANK provides the same numeric value for ties (for example 1, 1, 2, 4, 5).
SELECT *
FROM (
SELECT dept_id,
PersonName,
Salary,
RANK() OVER(PARTITION dept_id ORDER BY Salary DESC) AS SortBySalary
FROM employee_salary
)
WHERE SortBySalary = 1
Also, see this answer using MAX() function.
your table isn't clear to me. i can't understand why you're creating separate tables for all departments.
assuming you make two different tables, one for employees and one for department. this will make queries simpler for future and efficient database. In that case:
+----------+------------+------+-----+
|EmployeeID|EmployeeName|Salary|DepNo|
+----------+------------+------+-----+
| |
+----------+------------+------+-----+
+-----+-------+
|DepNo|DepName|
+-----+-------+
| |
+-----+-------+
SELECT DepName, EmployeeName, salary
FROM Department d
INNER JOIN Employee e on e.DepNo = d.DepNo
INNER JOIN
(
SELECT DepNo, MAX(salary) sal
FROM Employee
GROUP BY DepNo
) ss ON e.DepNo = ss.DepNo
AND e.salary = ss.sal;

SQLite SELECT not returning anything

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);

SELECT maximum and minimum value from a table

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.

Getting the correct SQL subquery where nested select statement has been used

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');