I have 3 tables:
Employee (EmployeeNo(PK),shopID(FK1) employeeName, employeeAddress)
Shop (shopID(PK), shopName, shopAddress)
Comment (EmployeeNo(FK), employeeComments)
Is it possible to run a query showing the names of all the employees who have left a comment in the comments table even if the employee name is not in the table?
It is actually a small query that you can easily learn.
Following query will return Employee Name who left the comment and the Comment left by him
SELECT employeeName, employeeComments
FROM Employee INNER JOIN Comment ON (Employee.EmployeeNo = Comment.EmployeeNo)
Select employeeName from employee,comment
where employee.employeeNo = comment.employeeNo
I think it is a simple join which you have to use.
SELECT E.employeeName
FROM Employee E
INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;
SELECT DISTINCT e.EmployeeName
FROM [Employee] AS e
RIGHT JOIN [Comment] AS c
ON e.EmployeeNo = c.EmployeeNo
OR
SELECT e.employeeName
FROM [Employee] AS e
WHERE e.EmployeeNo IN (
SELECT EmployeeNo
FROM [Comment]
)
Try this:
SELECT DISTINCT E.employeeName
FROM Employee E INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;
select e.employeeName,
c.employeeComment
from Employee e
right join Comment c on
e.EmployeeNo=c.EmployeeNo
this will give you employee name & comment
To display the employees who have commented we have following query:
SELECT DISTINCT e.employeeName
FROM Employee e
WHERE e.EmployeeNo IN (SELECT EmployeeNo
FROM Comment)
Related
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;
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
So i have an employees table:
I'm trying to create a query that displays all the info of this employee but i'm a little stumped at the Employee_Reports_To bit. What i want to appear is the First_Name and Last_Name of the Employee_ID that's stored in this column (Eg Joe Bloggs ID 1, reports to employee Joanne Blog ID 50)
Would this need a join or just a simple select. The database is having issues with the link when i try..
SELECT employee.*, departments.Department_Name,
jobTitle.Job_Title,
(SELECT manager.First_Name, manager.Last_Name
FROM Employee manager
INNER JOIN employee AS employeeReportsTo
ON manager.Employee_Reports_To = manager.Employee_ID
) AS Reports_To
FROM Employee employee
LEFT JOIN Departments AS departments
ON departments.Departments_ID = employee.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = employee.Job_Title_ID
ORDER BY `employee`.`Record_Active` DESC,
`employee`.`First_Name` ASC
Any assistance would be appreciated!
Try this solution:
SELECT emp.*,
manager.First_Name,
manager.Last_Name,
departments.Department_Name,
jobTitle.Job_Title
FROM Employee emp
INNER JOIN Employee manager
ON emp.Employee_Reports_To = manager.Employee_ID
LEFT JOIN Departments AS departments
ON departments.Departments_ID = emp.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = emp.Job_Title_ID
ORDER BY emp.Record_Active DESC,
emp.First_Name ASC
It's always better to use Join instead of using Sub Query.
EDITED:
Since column Employee_Reports_To can be null so it's better to use LEFT JOIN instead of INNER JOIN. Like This:
SELECT emp.*,
manager.First_Name,
manager.Last_Name,
departments.Department_Name,
jobTitle.Job_Title
FROM Employee emp
LEFT JOIN Employee manager --Changed Join here
ON emp.Employee_Reports_To = manager.Employee_ID
LEFT JOIN Departments AS departments
ON departments.Departments_ID = emp.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = emp.Job_Title_ID
ORDER BY emp.Record_Active DESC,
emp.First_Name ASC
Looking to your code you could use two time Employee (with alias e1 and e2) one for employee and one for the related manager
SELECT e1.*
, departments.Department_Name
, jobTitle.Job_Title
, e2.First_Name
,e2.Last_Name
FROM Employee e1
INNER JOIN Employee e2 ON e1.Employee_Reports_To = e2.Employee_ID
LEFT JOIN Departments AS departments
ON departments.Departments_ID = e1.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = e1.Job_Title_ID
ORDER BY e1.`Record_Active` DESC,
e1.`First_Name` ASC
I want each student's name, last payment date only. means only day.
I know i won't help you at all giving this code:
But you could try to learn something from it.
SELECT S.Id, S.Name, F.max_date, F.FeeAmt
FROM tbl_student As S
INNER JOIN (
SELECT t.Id, MAX(t.Date) As max_date, t.FeeAmt FROM tbl_fees As t GROUP BY t.Id
) As F ON F.Id=S.Id
First we selected all users from tbl_student, and then we are joining fees, selecting max date and grouping by user. The result is last (date) fee per user.
Please try this query. I hope this should give you the expected output:
SELECT S.Name, T1.LastPaymentDate
FROM
(SELECT Id, Max([Date]) AS LastPaymentDate from tbl_fees GROUP BY Id) AS T1
INNER JOIN
tbl_student AS S
ON T1.Id = S.Id
SELECT S.name,SUB.LAST_DATE
FROM tbl_student S
JOIN (SELECT f.id AS ID,MAX(f.Date) AS LAST_DATE
FROM tbl_fees f
GROUP BY f.id) SUB
ON SUB.id = S.id
I'm using MySQL and MSSql and I'm trying to join these two queries together.
Query 1
(SELECT REP.REP_NUM, REP.FIRST_NAME, REP.LAST_NAME
FROM REP, CUSTOMER)
Query 2
(SELECT CUSTOMER.REP_NUM, SUM(CUSTOMER.BALANCE) AS REP_BALANCE
FROM CUSTOMER
GROUP BY CUSTOMER.REP_NUM)
I've seen you can treat these as two Tables and join them but I'm having trouble getting it to work. The way I was trying to join them I'd get aggregate errors from trying to select the rep first and last name while using the balance sum.
Thanks in advance!
SELECT R.REP_NUM, R.FIRST_NAME, R.LAST_NAME
FROM REP r
inner join
(SELECT c.REP_NUM, SUM(c.BALANCE) AS REP_BALANCE
FROM CUSTOMER c
GROUP BY c.REP_NUM) t
on r.rep_num = t.rep_num
SELECT r.REP_NUM, r.FIRST_NAME, r.LAST_NAME, SUM (c.BALANCE) AS REP_BALANCE
FROM REP r
INNER JOIN CUSTOMER c ON r.REP_NUM = c.REP_NUM
GROUP BY r.REP_NUM, r.FIRST_NAME, r.LAST_NAME
try this:
SELECT REP.REP_NUM, REP.FIRST_NAME, REP.LAST_NAME
FROM REP join(
SELECT CUSTOMER.REP_NUM, SUM(CUSTOMER.BALANCE) AS REP_BALANCE
FROM CUSTOMER
GROUP BY CUSTOMER.REP_NUM
) as B on some_condition...
try it
select a.REP_NUM,a.FIRST_NAME,a.LAST_NAME,b.REP_NUM,Sum(b.BALANCE) as REP_BALANCE from REP a as inner join CUSTOMER b on a.REP_NUM=b.REP_NUM group by b.REP_NUM
Select New.REP_NUM,New.FIRST_NAME,New.LAST_NAME,CUSTOMER.REP_NUM,
SUM(CUSTOMER.BALANCE) AS REP_BALANCE
from (SELECT REP.REP_NUM, REP.FIRST_NAME, REP.LAST_NAME
FROM REP, CUSTOMER) New
inner join CUSTOMER ON CUSTOMER.REP_NUM=New.REP_NUM
GROUP BY CUSTOMER.REP_NUM