SQL error:Why test table doesn't exist? - mysql

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)

Related

How much efficient the nested Select query is?

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?

What is the difference between JOIN through ON and Using in MYSQL

I am trying to write a query to display the last name, department
number, and department name for all the employees.
And this my working code:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments USING(DEPARTMENT_ID);
When I was trying to make the query using JOIN ON ,I faced an error saying that
Column 'department_id' in field list is ambiguous through this code
query using JOIN ON:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments ON(
employees.DEPARTMENT_ID = departments.DEPARTMENT_ID
)
I want to know why it is not working .
The ambiguity should be resolved in select clause
SELECT
last_name,
employees.department_id,
department_name
FROM employees
JOIN departments USING(DEPARTMENT_ID);

How to get records by using 3 tables or work with 3 tables?

I have been asked this question in an interview. I have tried so hard, but unfortunately was not able to get it right.can anybody help me with this?
Retrieve the last name, first name, dept name of that employee using these tables.I am writing down the tables and columns.however, i am not writing that dummy data.
Employee- (id, last name, first name, DOB, SSN) and some other columns(not useful).
Dept - (D_id, dept name)
Emp_Dept - (id, D_id)
You can try this solution for your problem :
Query :
SELECT E.last_name, E.first_name, D.dept_name
FROM Employee AS E
-- Get employee dept
INNER JOIN Emp_Dept AS ED
ON ED.id = E.id
-- get dept data
INNER JOIN Dept AS D
ON D.id = ED.D_id
I hope it will help you.
But this query doesn't return value of employee without dept. If this is necessary you should use LEFT JOIN instead of INNER JOIN

Trouble with SQL exercise

I am trying to retrieve the full name (concatenated and with a column header), job title, department id, department name and salary for all employees from the database and nothing I try seems to work.
select first_name ||' '|| last_name "Full Name", department_id, salary from employees
This is as far as I got since the department_name is from the departments table and the job_title is from the jobs table. The rest is from the employees table and that's why this part works. can anyone help me?
Check out https://www.w3schools.com/sql/sql_alias.asp and search for "Alias Example for Tables"
select employees.first_name, employees.last_name, jobs.job_title, departments.department_name, employees.department_id
from employees, jobs, departments
where employees.job_id = jobs.job_id and employees.department_id = departments.department_id
its working now thank you !

SQL: return multiple rows with maximal value on a column

You can find the problem from here.
When there are multiple people with the highest salary in each department, the following SQL command output the expected results - all employees with highest salary are output:
select d.Name as Department, e.Name as Employee, e.Salary as Salary
from
(select Name, Salary, DepartmentId
from Employee
having (Salary, DepartmentId) in (
select max(Salary), DepartmentId
from Employee
group by DepartmentId)
)e, Department d
where e.DepartmentId = d.Id
However, the following command only output one employee even if there are multiple employees with the same highest salary:
select d.Name as Department, e.Name as Employee, e.Salary as Salary
from
(select Name, Salary, DepartmentId
from Employee
group by DepartmentId
having Salary = max(Salary)
)e, Department d
where e.DepartmentId = d.Id
Can anyone explain to me why the latter one does not work? Thanks!
The reason the second query doesn't work is because it's getting the max salary regardless of the department.
The way I would have done this, despite the valid first query (which is not using the standard JOIN syntax like it should), would be to first get the highest salary for each department like this:
SELECT departmentID, MAX(salary)
FROM employee
GROUP BY departmentID;
Once you have that, you can use a self join to only select the rows with that salary and department. This will include multiple people if they have the same salary:
SELECT e.*
FROM employee e
JOIN(
SELECT departmentID, MAX(salary) AS maxSalary
FROM employee
GROUP BY departmentID) tmp ON tmp.departmentID = e.departmentID AND tmp.maxSalary = e.salary
For further explanation, consider just the inner subquery you have:
SELECT name, department, salary
FROM employee
GROUP BY department
HAVING salary = MAX(salary);
The first problem with this is that you are selecting rows that are not consistent within your group. For each department id, you have multiple name values and multiple salary values, which will be chosen arbitrarily when you do the grouping. In my example, and the first, you can see that the grouping explicitly pulls the (departmentID, salary) pair for the max salary in each department, which the second one does not.
Here is an SQL Fiddle example to visually see the differences between your second query and mine.