Counting employees in country - user input not working - sql

I'm writing query that returns number of people working in country. User have to input COUNTRY_ID which he is interested in.
I wrote down query that works fine, but only with hardcoded COUNTRY_ID.
SELECT COUNT(e.EMPLOYEE_ID)
FROM EMPLOYEES e
JOIN DEPARTMENTS d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
JOIN LOCATIONS l ON d.LOCATION_ID = l.LOCATION_ID
JOIN COUNTRIES c ON l.COUNTRY_ID = c.COUNTRY_ID
WHERE c.COUNTRY_ID = 'US';
With hardcoded COUNTRY_ID query returns 68 rows, with user input query returns 0 rows.
c.COUNTRY_ID = '&c.COUNTRY_ID'
Maybe I'm using this function wrongly, I would be glad for solution. Thanks.

Related

how to use "not in" operator in oracle cmd?

I am using this code:
create or replace view VIEW_MAXMIN as
select c.country_name,
max(salary) max_salary,
min(salary) min_salary
from employees e,
departments d,
locations l,
countries c
where e.department_id = d.department_id
and d.location_id = l.location_id
and l.country_id = c.country_id = not in(select country_name
from countries
where country_name = 'Mexico');
but always error saying 'SQL command not properly ended'
You can change the joins from legacy Oracle comma joins to ANSI joins and then it looks like you want countries where the name is not Mexico.
Also, if you are aggregating columns and have a column that you are not aggregating then you need to use GROUP BY:
create or replace view VIEW_MAXMIN as
select c.country_name,
max(salary) max_salary,
min(salary) min_salary
from employees e
INNER JOIN departments d
ON (e.department_id = d.department_id)
INNER JOIN locations l
ON (d.location_id = l.location_id)
INNER JOIN countries c
ON (l.country_id = c.country_id)
where c.country_name != 'Mexico'
GROUP BY c.country_name;
Looks like the error you're getting in particular is due to -
l.country_id = c.country_id = not in(select country_name
from countries
where country_name = 'Mexico');
You have 2 equals to condition in one filter statement - like a = b = c
Just - l.country_id = c.country_id would suffice and country_name != 'Mexico' will have to be a separate condition

Group by Country with bridge table

I am not quite sure with the tables below to write a query that returns countries that have communities. The CommunityLocation bridge table is where I can look for companies and their locations but not quite sure how to really mold this query.
Community
community_id
Location
location_id
country_id
Country
country_id
CommunityLocation
community_id
location_id
So I just need a list of country IDs that have communities.
I think this is what you need:
select distinct
c.country_id
from
countires c
join Location l
on c.country_id = l.country_id
join CommunityLocation cl
on cl.location_id = l.location_id
I would recommend exists:
select c.country_id
from countries c
where exists (select 1
from Location l join
CommunityLocation cl
on cl.location_id = l.location_id
where c.country_id = l.country_id
);
This avoids extra processing for removing duplicates -- and that is usually a performance gain.

SQL QUERY: The locations of each department and the number of employees working in each location

I'm trying to solve this query, I have to find the locations for each department and the number of employees per location (not for department, common query for this database)
SELECT DE.DLOCATION, count(E.DNO)
FROM DEPARTMENT D
INNER JOIN EMPLOYEE E ON D.DNUMBER = E.DNO
INNER JOIN DEPT_LOCATIONS DE ON DE.DNUMBER = E.DNO
GROUP BY DE.DLOCATION
/
This works but this shows me the number of employees per department (with possible repetitions of the value)
How can i get the number of employees for locations and not for department?
EDIT: The query works, i actually get the number of employees per location but in every city there is more than one department so if in the 'software' department of Houston there are 14 employees, then again in the 'software' department of Sugarland it shows me 14 employees so the query works but not per location, it works per department. Thank you anyway for your help i appreciate it
You are closer to the solution. Since you haven't posted about other columns in these tables like employee ID or number. Going by what you have, try:
SELECT E.DNO, DE.DLOCATION, count(E.DNO)
FROM DEPARTMENT D
INNER JOIN EMPLOYEE E
ON D.DNUMBER = E.DNO
INNER JOIN DEPT_LOCATIONS DE
ON DE.DNUMBER = E.DNO
GROUP BY E.DNO,DE.DLOCATION
Your query should work as written what you need is distinct if i am not wrong.
SELECT DE.DLOCATION, COUNT(distinct E.DNO) as NoOfEmps
FROM DEPARTMENT D INNER JOIN
EMPLOYEE E
ON D.DNUMBER = E.DNO INNER JOIN
DEPT_LOCATIONS DE
ON DE.DNUMBER = E.DNO
GROUP BY DE.DLOCATION;
Perhaps you need :
SELECT DE.DLOCATION, COUNT(*) as NoOfEmps
. . .
SQL> SELECT D.loc, COUNT(E.Deptno) as NoOfEmps
2 FROM DEPT D INNER JOIN EMP E
3 ON D.DEPTNO = E.DEPTNO
4 GROUP BY D.LOC;
LOC NOOFEMPS
------------- ----------
NEW YORK 3
CHICAGO 6
DALLAS 5

Oracle sql scripting. Querying. List the locations the company has employees located at

Hey I am struggling with a question, I am a beginner at database management. I have the following database, and I want to make this query: List the locations the company has employees located at and how many employees are at each location ordered by the location with the most employees in descending
Anybody help please!
this is the database
I believe this gets what you're looking for.
Select l.location_id, c.country_name, count(e.employee_id) as employee_count
From locations l
left join countries c ON c.country_id = l.country_id
left join departments d ON l.location_id = d.location_id
left join employees e ON e.manager_id = d.manager_id
GROUP BY l.location_id, c.country_name
ORDER BY employee_count DESC;

Joining 3+ tables with join syntax in Oracle

I've been using joins for a long time, but it's always been the old syntax. Now I'm trying to figure out how to do basic inner joins with the JOIN syntax, and having trouble figuring it out.
Let's say I have 3 tables.
Employees:
EmployeeID EmployeeName DepartmentID
1 John Smith 2
2 Jane Doe 3
3 Mark Brown 1
Departments:
DepartmentID DepartmentName AreaID
1 Sales 2
2 Marketing 1
3 Opeations 3
Areas:
AreaID AreaName
1 Marketing
2 Sales
3 Opeartions
I need to get a list of all employee names with their departments and areas.
With the old syntax, I'd run the following query:
select e.EmployeeName, d.DepartmentName, a.AreaName
from employees e, departments d, areas a
where e.DepartmentID = d.DepartmentID
and d.AreaID = a.AreaID
With the new syntax it seems that I can only join employees with departments, but not departments with areas in the same query. Or should I perhaps use a subselect?
Try this.
select e.EmployeeName, d.DepartmentName, a.AreaName
from employees e join departments d on e.DepartmentID = d.DepartmentID
join areas a on d.AreaID = a.AreaID;
I find this explanation of joins to be pretty useful.
Your query will look like this:
SELECT e.EmployeeName, d.DepartmentName, a.AreaName
FROM employees e
INNER JOIN departments d on e.DepartmentID = d.DepartmentID
INNER JOIN areas a on d.AreaID = a.AreaID
You can try left joining the Employees, Departments, and Areas tables, in that order. We are meticulous here at Stack Overflow, and so we chose LEFT JOIN rather than INNER JOIN because it handles the possibility that an employee may not have been assigned to a department, in which case the query would display NA as the value.
SELECT e.EmployeeName,
COALESCE(d.DepartmentName, 'NA'),
COALESCE(a.AreaName, 'NA')
FROM Employees e
LEFT JOIN
Departments d
ON e.DepartmentID = d.DepartmentID
LEFT JOIN Areas a
ON d.AreaID = a.AreaID
The code that you've written is an alternative code for Inner Join.
You can use the code below.
So what I've done is Joined the Employee table to the Department table using the DepartmentID and Joined the Employee table to the Areas table using the AreaID.
Select e.EmployeeName
, d.DepartmentName
, a.AreaName
from employees e
INNER JOIN departments d
, areas a
ON e.DepartmentID = d.DepartmentID
INNER JOIN areas a
ON d.AreaID = a.AreaID