SQL query to find names of employees belonging to particular department - mysql

I have created two tables one employee table and the other is department table .Employee table has fields EmpId , Empname , DeptID , sal , Editedby and editedon where
EmpId is the primary key and Dept table has DeptID and deptname where DeptID is the secondary key.
I want the SQL query to show names of employees belonging to software departmant
The entries in dept table are as below :
DeptID Deptname
1 Software
2 Accounts
3 Administration
4 Marine

Use INNER JOIN:
SELECT
E.empname
FROM Employee E
INNER JOIN department D ON E.DeptID=D.DeptID
WHERE D.DeptID = '1'

Is this what you need?
SELECT EmpName FROM Employee WHERE DeptID = 1

Try this:
SELECT
E.empname
FROM Employee E
INNER JOIN department D ON E.DeptID=D.DeptID
where D.Deptname = 'Software'

Related

sql query question makes me confused please

Given the following two relational schemas, the EMPLOYEE relational schema records the employee number (ssn), age (age) and the department (dno), the primary key is ssn, and the DEPARTMENT relational schema records the department number (dnum ) and name (dname), The primary key is dnum, in which the foreign key "dno" of EMPLOYEE refers to the primary key "dnum" of DEPARTMENT,
please use SQL GROUP BY to write the query ''For each department that has more than two employees, retrieve the department name and the number of its employees who each is more than twenty-five years old)”.
Thank you guys
You can use as approach. It's not exactly your tables but you can easy match it tot your structure:
SELECT DEPARTMENT_NAME
FROM DEPARTMENTS D
JOIN EMPLOYEES E USING (DEPARTMENT_ID)
GROUP BY DEPARTMENT_ID, DEPARTMENT_NAME
HAVING COUNT(EMPLOYEE_ID) > 2
;
format sql online
SELECT d.dname, count(e.ssn) as cnt
FROM Employee e JOIN department d
on e.dno = d.dnum
WHERE e.age> = 25
Group BY e.dname
HAVING COUNT(ssn) >= 2;

How to use COUNT() and GROUP BY to display number of employees in each department

Please consider the two tables - Employee and Department
Employee:
EmployeeID
Name
DeptID
Department:
DepartmentID
DeptName
LocID
Employee.DeptID is a foreign key to Department.DepartmentID
How would one display a list of all departments (Department Names) and the number of employees in each department? The output should look like this:
DepartmentName Number of employees
Accounts 30
HR 24
Production 400
Sales/Marketing 250
etc...
Use GROUP BY
SELECT d.deptID, count(e.deptID)
FROM Department d
LEFT JOIN Employee e ON d.DeptID = e.DeptID
GROUP BY d.deptId
and LEFT JOIN is used to include departments that do not have employees.
SELECT DeptName AS DepartmentName, COUNT(EmployeeID) AS NumberOfEmployees FROM Employee INNER JOIN Department ON DeptID = DepartmentID GROUP BY DepartmentID

Conditional SQL

I just recently started learning how to write SQL queries, and I have a lot to learn and a lot of questions, mainly regarding defining conditions for an SQL query. I have 3 tables (with fields listed below):
Employee:
EmployeeID, Name, DoB, StartDate
Salary:
SalaryID, DataPaid, AmountPaid, EmployeeID
Address:
AddressID, Address, City, EmployeeID
Now I would like to know how to:
1. Count the number of employees that live in the city of London.
My attempt:
SELECT COUNT(City) AS EmployeeID FROM Address
WHERE City='London';
2. Add up the 'AmountPaid' values for all employees from the city of London.
My attempt:
SELECT SUM(AmountPaid) AS TotalPaid FROM Salary
WHERE City='London';
3. Display data for all employees that started in 2012 (have a 'Start Date' containing 2012). Not sure where to begin with this!
4. Delete all records where the employee 'Name' field is empty/null.
My attempt:
DELETE FROM Employee
WHERE Name=NULL;
Am I doing something wrong with my attempts? Any help would be appreciated.
THANK YOU!
In SQL Server (T-SQL), you cannot test a value for NULL using '='. It must be as follows:
Delete From Employee Where Name IS NULL
Also i would check for empty names:
DELETE FROM Employee where Name IS NULL OR Name=''
As for the point 3:
SELECT * FROM Employee WHERE Year(StartDate)=2012
As for the point 2, the table Salary does not have an City column. You need to join with Employee table like this:
SELECT SUM(AmountPaid) AS TotalPaid FROM Salary SA inner join Employee Em on Em.EmployeeID=SA.EmployeeID WHERE Em.City='London';
Smells a bit like homework. Anyway:
(1) count of employees in the city of London
First statement seems correct to me, isn't it? But the alias confused me a bit as it says EmployeeID but returned value is the count of employees.
(2) total amount paid for employees in the city of London
This wouldn't work with your statement because table Salary has no field City. You'll need a join here:
select sum(AmountPaid) as TotalPaid
from Salary s
join Address a on s.EmployeeID = a.EmployeeID
where a.City = 'London'
(3) employees started in 2012
Here you can use YEAR function to extract the year out of the date:
select *
from Employee
where Year(StartDate) = 2012
(4) delete unnamed employees
Taken from the solution of #ericpap (for the sake of completeness):
delete from Employee
where Name is NULL
or Name = ''
I would do it like this - it includes a test whereby you make tables and data to help you see how it works;
I assume a employee has only one address, but recieves multiple payments and while it may not be necessary to join from the employee tables in your example - it shows how the relationships between employees and the other tables work, which may help you understand the relational model better.
--Make the Tables
CREATE TABLE dbo.Employee
(
EmployeeID BIGINT PRIMARY KEY NOT NULL,
Name VARCHAR(50),
DoB DATE,
StartDate DATE
)
CREATE TABLE dbo.Salary
(
SalaryID BIGINT PRIMARY KEY NOT NULL,
DatePaid DATE, -- I think you mean DatePaid and not DataPaid
AmountPaid MONEY,
EmployeeID BIGINT
)
CREATE TABLE dbo.Address
(
AddressID BIGINT PRIMARY KEY NOT NULL,
[Address] VARCHAR(max),
City VARCHAR(200),
EmployeeID BIGINT
)
-- Put in some Test Data
INSERT INTO dbo.Employee (EmployeeID,Name, DoB, StartDate)
VALUES (1,'Bill Gates','19551028','20121014'),
(2,'Larry Ellison','19440817','20140101')
INSERT INTO dbo.Address (AddressID,[Address], City, EmployeeID)
VALUES (1,'15 Microsoft House','New York',1),
(2,'23 Oracle Flats','London',2)
INSERT INTO dbo.Salary(SalaryID, DatePaid, AmountPaid, EmployeeID)
VALUES (1,Getdate(),5000.53,1),
(2,'20140201',10000.23,2),
(3,'20140301',10000.23,2)
-- Queries;
--Count the number of employees that live in the city of London.
SELECT COUNT(Distinct E.EmployeeID) as Count_London
FROM dbo.Employee E
INNER JOIN dbo.[Address] A
ON E.EmployeeID = A.EmployeeID
WHERE A.City = 'London'
-- Result = 1
-- 2. Add up the 'AmountPaid' values for all employees from the city of London
--Total Amount
SELECT Sum(S.AmountPaid) as TotalPaid
FROM dbo.Employee E
INNER JOIN dbo.[Address] A
ON E.EmployeeID = A.EmployeeID
LEFT JOIN dbo.Salary S
ON E.EmployeeID = S.EmployeeID
WHERE A.City = 'London'
-- Result = 20000.46 (2 x 10000.23)
--split by employee;
SELECT E.EmployeeID,E.Name,Sum(S.AmountPaid) as TotalPaid
FROM dbo.Employee E
INNER JOIN dbo.[Address] A
ON E.EmployeeID = A.EmployeeID
LEFT JOIN dbo.Salary S
ON E.EmployeeID = S.EmployeeID
WHERE A.City = 'London'
GROUP BY E.EmployeeID, E.Name
--3. Display data for all employees that started in 2012
SELECT *
FROM dbo.Employee E
INNER JOIN dbo.[Address] A
ON E.EmployeeID = A.EmployeeID
LEFT JOIN dbo.Salary S
ON E.EmployeeID = S.EmployeeID
WHERE StartDate >= '20120101' AND StartDate < '20130101'
-- result = all bill gates fields
-- 4. Delete all records where the employee 'Name' field is empty/null.
DELETE FROM dbo.Employee WHERE EmployeeID IS NULL
you might want to delete where the employeeID is null from all the tables if it is possible to have records in there
This wasn't addressed in the other answers:
Count the number of employees that live in the city of London
Your attempt below will get you the right answer only if there is a different employee per row.
SELECT COUNT(City) AS EmployeeID FROM Address
WHERE City='London';
If your Address table looks like it does below, using the query you wrote, you would get 3 employees (but we can see from the table there are only 2 distinct EmployeeID's so thus only 2 employees).
AddressID, Address, City, EmployeeID
1, 1 Main St, London, 1
2, 2 Main St, London, 2
3, 3 Main St, London, 1
What you should do is count the number of distinct EmployeeID's. Each of these EmployeeID's identifies a unique employee. Thus your query should look like this:
SELECT COUNT(DISTINCT EmployeeID) AS EmployeeID FROM Address
WHERE City='London';
That will get you the correct answer every time regardless of what your table looks like.

SQL count with another table in SELECT clause

In my database, have 2 different fields
i.) Employee
ii.) Department
In my employee table,
NAME Department
---------------------
John IT
Siti Research
Jason Research
In my Department,
Name
------------
IT
Research
Computer
Using statement
SELECT DEPARTMENT.DNAME
FROM DEPARTMENT,
EMPLOYEE
WHERE DEPARTMENT.DNAME = EMPLOYEE.DNAME
AND
(SELECT COUNT(*)
FROM EMPLOYEE.DNAME)=0;
when no employee in the department then will display
Name
--------------
Computer
Keep trying but having some error on it
Two alternatives:
Using IN:
SELECT name FROM Department
WHERE name NOT IN (SELECT DISTINCT Department
FROM Employee)
Using Left Join:
SELECT D.NAME
FROM DEPARTMENT D LEFT JOIN EMPLOYEE ON D.NAME = EMPLOYEE.Department
WHERE EMPLOYEE.Department IS NULL
An example in Fiddle.
This method will show higher performance than the other if you have thousands of records in your table.
Try NOT IN. Sub query need to be DISTINCT to avoid performance issue in future:
SELECT name FROM Department
WHERE name NOT IN ( SELECT DISTINCT Department FROM Employee);
Or NOT EXIST, faster in most cases:
SELECT name FROM Department
WHERE NOT EXIST ( SELECT 1 FROM Employee
WHERE Employee.Department = Department.name);
You don't state your error but you can select all departments that don't appear in the employee table:
SELECT DEPARTMENT.DNAME
FROM DEPARTMENT
WHERE DEPARTMENT.DNAME NOT IN (SELECT DEPARTMENT FROM EMPLOYEE);
SELECT D.Name
FROM Employee E
RIGHT JOIN Department D
ON E.Department=D.Name
WHERE E.Department IS NULL
SQL Fiddle DEMO

select employee_name, count(dependent) as dependents from employee error

i have this two tables "employee table, dependent table"
the employee table has "ID, employee_id, employee_name"
the dependent table has "ID, employee_id, dependent_name, relationship"
and the value of employee table is ("1, 123, Vincent")
and the value of dependent table is
("1, 123, Angel, daughter"),("2, 123, Mary, daughter")
how will i display an employee_id and employee_name and it's number of dependents?
like select employee_id, employee_name , count(dependent) as dependents ???
Try this query:
select e.employee_id,e.employee_name,count(d.employee_id) as dependents
from department d
inner join employee e on (e.employee_id=d.employee_id)
group by d.employee_id;
Sql Fiddle Example
select e.employee_id, e.employee_name , (select count(*) from
dependent_table where employee_id=e.employee_id) as dependents
from employee_table as e
select
e.employee_id,
e.employee_name,
count(select * from dependent
table where
dependent table.employee_id=employee table.employee_id)
from
employee table e
where e.employee_id=123
try this
try this its worked
select a.empid ,a.empname , count(b.deptname) from employee a , dependent b
where a.empid = b.empid and b.empid=123;
Use Group By and Count
select
em.Employee_id,
count(*)
from
employee em inner join
dependant dp on
em.employee_id = dp.employee_id
group by
em.employee_id