I have two tables: employees and offices.
I want to create a view with all the columns from 'employees', but only two columns of 'offices'.
Also, I want to select only employees who have unique job titles. I'm trying to do it with the following code, but it returns the following error:
#1248 - Every derived table must have its own alias.
I'm using the following query:
SELECT employees.*, offices.officeCode, offices.phone
FROM (
SELECT DISTINCT employees.jobTitle
)
JOIN offices ON employees.officeCode = offices.officeCode
offices table:
employees table:
Desired result:
employeeNumber|jobTitle|firstName|officeCode|city|state|country
including only the first 6 employees from the sample image (as 'Sales Rep' is a repeated jobTitle, the employees with it wouldn't be included).
Current query has a number of issues:
As error indicates, derived table or subquery does not have an alias.
Incomplete SELECT query in derived table without FROM clause: (SELECT distinct emplyees.jobtitle)
Retrieving columns from a table not referenced in data sources of query (i.e., employees)
Therefore, consider joining the two tables with a count check on unique job title:
SELECT e.*, o.officeCode, o.phone
FROM employees e
INNER JOIN offices o
ON e.officeCode = o.officeCode
WHERE e.jobTitle IN
(SELECT sub.jobTitle
FROM employees sub
GROUP BY sub.jobTitle
HAVING COUNT(*) = 1)
With this query:
SELECT e.*
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM employees WHERE employeeNumber <> e.employeeNumber AND jobTitle = e.jobTitle)
you get all the employees with jobTitle that does not have any other employee.
So join it to offices:
SELECT e.employeeNumber, e.jobTitle, e.firstName, o.*
FROM (
SELECT e.*
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM employees WHERE employeeNumber <> e.employeeNumber AND jobTitle = e.jobTitle)
) e INNER JOIN offices o
ON e.officeCode = o.officeCode
Just use window functions:
SELECT e.*, o.officeCode, o.phone
FROM (SELECT e.*, COUNT(*) OVER (PARTITION BY jobTitle) as job_cnt
FROM employees e
) e JOIN
offices o
ON e.officeCode = o.officeCode
WHERE job_cnt = 1;
Related
I have the below table, with empid, deptid, and Name. I need to get the results as
empid,deptid,name and count of employees in each department.
CREATE TABLE emp(empid INTEGER PRIMARY KEY, deptid INTEGER, NAME TEXT);
/* Create few records in this table */
INSERT INTO emp VALUES
(1,100,'Tom'),
(2,200,'Lucy'),
(3,300,'Frank'),
(4,100,'Jane'),
(5,400,'Robert');
I need to get the results as empid,deptid,name, and count of employees in each department as below.
I am able to achieve results using the below queries.
SELECT a.empid, a.deptid, a.Name, result.emp_dept_count FROM emp a,
( SELECT b.deptid, COUNT(b.deptid) AS emp_dept_count FROM emp b
GROUP BY b.deptid ) result
WHERE a.deptid = result.deptid;
/* using common table expression */
WITH emp_dept_count_cte(deptid,emp_dept_count) AS ( SELECT b.deptid, COUNT(b.deptid) AS emp_dept_count FROM emp b
GROUP BY b.deptid )
SELECT a.empid, a.deptid, a.Name, result.emp_dept_count
FROM emp a, (SELECT deptid, emp_dept_count FROM emp_dept_count_cte) result
WHERE a.deptid = result.deptid;
/* using common table expression */
WITH emp_dept_count_cte (deptid,emp_dept_count) AS ( SELECT b.deptid, COUNT(b.deptid) AS emp_dept_count FROM emp b
GROUP BY b.deptid )
SELECT a.empid, a.deptid, a.Name, emp_dept_count_cte.emp_dept_count
FROM emp a
INNER JOIN emp_dept_count_cte
ON a.deptid = emp_dept_count_cte.deptid;
/* using common table expression */
WITH emp_dept_count_cte (deptid,emp_dept_count) AS ( SELECT b.deptid, COUNT(b.deptid) AS emp_dept_count FROM emp b
GROUP BY b.deptid )
SELECT a.empid, a.deptid, a.Name, emp_dept_count_cte.emp_dept_count
FROM emp a
LEFT JOIN emp_dept_count_cte
ON a.deptid = emp_dept_count_cte.deptid;
Is it possible to do this in alternate ways?
No need for CTE. Just do
SELECT *, COUNT(1) OVER (PARTITION BY deptid) AS emp_dept_count FROM emp
You are describing a window count:
select e.*,
count(*) over(partition by deptid) as emp_dept_count
from emp e
In pre-8.0 MySQL versions, where window functions are not supported, you can use a correlated subquery:
select e.*,
(select count(*) from emp e1 where e1.deptid = e.deptid) as emp_dept_count
from emp e
Or you can join an aggregate query:
select e.*, d.emp_dept_count
from emp e
inner join (select deptid, count(*) as emp_dept_count from emp group by deptid) d on d.deptid = e.deptid
This was probably asked a few times, but I couldn't find an answer. While doing JOIN on two tables, I encounter a problem.
First table is named "Employees" and another "Orders".
I'm trying to create a query, which will give me the output like:
order_id (from Orders) | first_name (from Employees) | last_name (from Employees)
The queries I use are:
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID;
or full join:
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID
UNION
SELECT * FROM Employees
RIGHT JOIN Orders o on Employees.EmployeeID = o.employeeid;
both work just fine, giving me the same results. Unless I select which columns I wish to extract. So query like that:
SELECT Orders.orderid, e.first_name, e.last_name FROM orders
LEFT JOIN Employees e on orders.employeeid = e.EmployeeID;
Gives me totally different results. Eg. first 100 orderids have same employee name, then another 100 different one and so on (only 4 employees overall, should be 9).
What am I doing wrong?
EDIT (screenshots added):
Orders table:
Employees table:
Output when doing full join, everything seems to be ok:
Left join (or any other join, looks the same). Some orders seem to be ommited, but overall only 4 employees are listed.
I don't know your data, but I think you want:
SELECT o.orderid, e.first_name, e.last_name FROM orders o INNER JOIN Employees e ON o.employeeid = e.employeeId
where o.employeeid != null
Order by o.orderid
It's hard to tell without seeing some data samples.
But here are a few points:
With LEFT JOIN you get 'only 4 employees overall, should be 9' -- I assume there are 5 employees that do not have any order.
In the 'full join' you get mixed columns, the correct way should be
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID
UNION
SELECT * FROM Orders
RIGHT JOIN Employees e on Orders.employeeid = e.EmployeeID
Otherwise there should be no difference in the output if you specify the columns, as opposed to *.
If
SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID
is returning the correct set of results, then this should probably work:
Select orderid, FirstName, LastName from (SELECT * FROM Orders
LEFT JOIN Employees e on Orders.employeeid = e.EmployeeID LIMIT 999999) as joinedTable
I have 2 tables in my database.
Table 1. employee
id
name
department_id
Table 2. department
id
name
What will be the query to fetch all employees with their department?
So I have written this query
SELECT employee.name
, department.name
FROM employee
JOIN department
ON employee.department_id = department.id
And this seems to be correct but I am not able to write a query if I want to fetch only the department that has the highest number of employees. How can I achieve this?
To guarantee just one department...
SELECT
*
FROM
department
WHERE
id = (SELECT department_id
FROM employee
GROUP BY department_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
Note, if two departments are tied with joint maximum employees, this will still only select One of them (arbitrarily chosen, potentially different each time).
To handle ties, you could do the following...
SELECT *
FROM department
WHERE id IN (SELECT department_id
FROM employee
GROUP BY department_id
HAVING COUNT(*) = (SELECT COUNT(*)
FROM employee
GROUP BY department_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
)
This is a real pain to handle in MySQL. Here is one option:
SELECT d1.id, d1.name
FROM department d1
INNER JOIN employee e1
ON d1.id = e1.department_id
GROUP BY d1.id, d1.name
HAVING COUNT(*) = (SELECT COUNT(*) FROM department d2 INNER JOIN employee e2
ON d2.id = e2.department_id
GROUP BY d2.id ORDER BY COUNT(*) DESC LIMIT 1);
Note that if you were using a database with analytic function support, such as SQL Server, then the problem gets much easier:
SELECT id, name
FROM
(
SELECT d.id, d.name, DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) dr
FROM department d
INNER JOIN employee e
ON d.id = e.department_id
GROUP BY d.id, d.name
) t
WHERE t.dr = 1;
This question can be solved in multiple ways:
Using sub query
SELECT name FROM department
WHERE id IN
(SELECT department_id FROM employee HAVING COUNT(department_id)
IN
(SELECT MAX(COUNT(department_id)) FROM employee) GROUP BY department_id)
Using Join
SELECT name FROM employee e
INNER JOIN
department d ON e.department_id = d.id
HAVING COUNT(e.department_id)
IN
(SELECT MAX(COUNT(department_id)) from employee) group by department_id)
first check the column related two types have same name, same data type and the use subquery
SELECT name
FROM department
WHERE id IN (
SELECT department_id
FROM employees
HAVING COUNT(department_id) IN (
SELECT MAX(COUNT(dept_id))
FROM employees
)
GROUP BY department_id
)
Your query should work for the first question.
for the second, You can use this. The sub query would give you the dept Id for the most employees, which the outer query would give additional details for.
select * from department where department_id in
(select limit 1 Employee.department_id from Employee group by department_id
order by count(Employee.name) desc)
I have 2 tables employees(id, first_name, last_name, salary, department_id_ and department(id, name) and I want to show number of employees in each department.
I have this question here:
SELECT department.name, COUNT(*) AS 'employees_number'
FROM department
LEFT JOIN employees
ON employees.department_id = department.id
GROUP BY department.id, department.name;
But for some reason, in departments where I have no people, it shows a number of employees as 1. Any idea why this is happening?
With an outer join you still get a result row when no match in the outer table is found. Only all employee column values are null then.
So rather than count the records, you want to count matched records, i.e. where an employee was found and its data is not null. So Count a column in the employee table (nulls are not counted, when counting a column or expression). E.g. use COUNT(e.department_id) or COUNT(e.id):
SELECT d.name, COUNT(e.id) AS employees_number
FROM department d
LEFT JOIN employees e ON e.department_id = d.id
GROUP BY d.id, d.name;
What I prefer though, is to aggregate/count before joining. The query looks a bit more complicated, but is less prone to errors on future query changes:
SELECT d.name, COALESCE(e.how_many, 0) AS employees_number
FROM department d
LEFT JOIN
(
SELECT department_id, COUNT(*) AS how_many
FROM employees
GROUP BY department_id
) e ON e.department_id = d.id;
As it's one aggregated column only you want, you can move the subquery to your SELECT clause and get thus a simpler query:
SELECT
d.name,
(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.id
) AS employees_number
FROM department d;
Using SUM instead of COUNT also can give you what you want:
SELECT
department.name,
SUM(CASE WHEN employees.id IS NOT NULL THEN 1 ELSE 0 END) AS 'employees_number'
FROM department
LEFT JOIN employees
ON employees.department_id = department.id
GROUP BY department.id, department.name;
SQL Fiddle:
http://sqlfiddle.com/#!9/8b8976/1
select department.name, count(employee.id) as co from
department left join employee on
department.id = employee.dept_id group by department.name
order by co desc, department.name asc
I have the folowing tables.
ORDER
OrderNumber
CustomerNumber
EmployeeNumber
OrderDate
CUSTOMER
CustomerNumber
Name
Address
EMPLOYEE
EmployeeNumber
Name
Address
ORDERDETAIL
OrderNumber
Qty
Description
Price
Let say ORDERDETAIL table has 10 records
I would like to write a query that will return 10 records from ORDERDETAIL table to include Employee name, employee address, customer name, customer address and and order Date.
I know that I could write a query and use INNER JOIN to get the info from ORDER table, but how do you create the rest of query to get the info from the CUSTOMER and EMPLOYEE tables.
SELECT *
FROM OrderDetail D
INNER JOIN Order O
ON D.OrderNumber = O.OrderNumber;
Just add some more joins...
SELECT *
FROM OrderDetail D
JOIN Order USING (OrderNumber)
JOIN Customer USING (CustomerNumber)
JOIN Employee USING (EmployeeNumber)
You might want to re-order the JOINs in order to have the smallest tables first, as this could provide you with some performance boost (depending on your server's version, the most recent will optimize the join for you and might actually execute the joins in the "probably best" way).
Also, in the MySQL dialect at least, JOIN implicitly expands to INNER JOIN, and writing
A JOIN B USING (COL)
is equivalent to writing
A JOIN B ON (A.COL = B.COL)
SELECT *
FROM OrderDetail D
INNER JOIN Order O ON D.OrderNumber = O.OrderNumber
INNER JOIN Eployee E on O.EployeeNumber = E.EployeeNumber
INNER JOIN Customer C on O.CustomerNumber = C.CustomerNumber
if you have foreign key reference between
ORDER.EmployeeNumber and EMPLOYEE.EmployeeNumber
ORDER.CustomerNumber and CUSTOMER.CustomerNumber
then try this
SELECT
E.name AS employeeName,
E.Address AS employeeAddress,
C.name AS customerName,
C.Address AS customerAddress.
O.OrderDate
FROM OrderDetail D
INNER JOIN Order O ON D.OrderNumber = O.OrderNumber
INNER JOIN EMPLOYEE E ON E.EmployeeNumber = 0.EmployeeNumber
INNER JOIN CUSTOMER C ON C.CustomerNumber= 0.CustomerNumber
LIMIT 0,10