Get the names of the employees who are working on the project named “Office Security Project” and their responsibilities in the project
Give the qualification for the above query in:
1) conjunctive normal form, and
2) disjunctive normal form.
Consider the following relations
EMP (eNo, eName, title)
ASG (eNo, pNo, resp, dur)
PROJ (pNo, pName, budget, loc)
Query: Get the names of the employees who are working on the project named “Office Security Project” and their responsibilities in the project.
The query expressed in SQL is :
SELECT ENAME, RESP
FROM EMP, ASG, PROJ
WHERE EMP.ENO = ASG.ENO AND ASG.PNO = PROJ.PNO AND PNAME = “OFFICE SECURITY PROJECT’
To get data you should use select
SELECT employee,responsibility FROM table_name WHERE project_name='Office_Security_Project';
Related
I'm having the schemas like:
Employee (Empno, Empname, City)
Project (Pno, Pname)
Part (Partno, Partname, Color)
Use (Pno, Partno)
Works (Empno, Pno)
From these schemas I had created a sample tables:
The goal is to find the names of the projects where no employees are working whose name starts with 'S'
I'm using ORACLE 11g Express Edition.
Here I used this query :
For Names:
Select DISTINCT Pname FROM
(
SELECT w.Empno, p.Pno, p.Pname, e.Empname
FROM Works w
LEFT JOIN Project p ON w.Pno=p.Pno
LEFT JOIN Employee e ON e.Empno=w.Empno
)
WHERE Empname not like 'S%';
If you think about how to explain the process. There are several methods to solve this including:
You would start with each of the Projects and find out if there does not exist anybody who Works on the project where the Employees name starts with S. You can do this using NOT EXISTS.
or
Again, start with a Project and find, if any, who Works on the project and their corresponding Employees details using LEFT OUTER JOINs (but starting from the Project) and filtering for employee names starting with S in the JOIN condition. Then GROUP BY the primary key for the project and find those projects HAVING a COUNT of zero matched employees.
Since this appears to be a homework question, I'll leave the rest for you to complete.
I'm having much trouble with the Oracle Live SQL tutorials for the Oracle Apex Workspace. In particular, I'm stuck on Module 3: Inserting data because of the following:
The tutorial wants me to run the following code to add to the employees table:
insert into EMPLOYEES
(name, job, salary, deptno)
values
('Sam Smith','Programmer',
5000,
(select deptno
from departments
where name = 'Development'));
insert into EMPLOYEES
(name, job, salary, deptno)
values
('Mara Martin','Analyst',
6000,
(select deptno
from departments
where name = 'Finance'));
insert into EMPLOYEES
(name, job, salary, deptno)
values
('Yun Yates','Analyst',
5500,
(select deptno
from departments
where name = 'Development'));
However, when I attempt to run this code (which was copied and pasted from the tutorial into my Oracle Apex Workspace), I get the following error:
ORA-01427: single-row subquery returns more than one row ORA-06512: at "SYS.DBMS_SQL", line 1721
I checked my Objects aka tables to ensure that there were no tables with duplicate column names, however, I don't have any tables with any duplicates.
Can anyone help me with this? I'm quite new to both SQL and the Oracle Apex workspace.
This suggests that your departments has more than one row for certain names. You can determine which:
select name
from departments
group by name
having count(*) > 1;
There probably should not be duplicates. You probably accidentally loaded the data twice into the table, or something like that.
Is below example good solution to get in one row all employee where job = 'MANAGER'
with cte as (
Select
job,
case when SAL = 2850 then ename end as SAL_1,
case when SAL = 2450 then ename end as SAL_2,
case when SAL = 2975 then ename end as SAL_3
from
(Select * from emp where job = 'MANAGER')
)
Select job,MIN(SAL_1) as SAL_1,MIN(SAL_2) as SAL_2,MIN(SAL_3) as SAL_3 from cte
group by job
No, this isn't a good way to show one row with all managers. Your query can only show up to three managers, and it doesn't even guarantee to show any manager at all. This depends on whether they happen to have one of the three salaries or not.
When writing a query you don't know how many managers there will be (and how much they earn) at the time someone runs the query. But in SQL queries you must state which (this includes "how many") columns to select.
Three options:
Just select rows (select * from emp where job = 'MANAGER') and let your app care about the display (i.e. loop through the rows and fill a grid with columns accordingly). This is the typical solution.
Count managers (select count(*) from emp where job = 'MANAGER'). Then build a separate query with as many columns as there are managers found and run this query. This technique is called "dynamic SQL". You can do this from your app or use Oracle's built-in programming language PL/SQL.
Put all the names into one column, i.e. have a string column that contains all names separated by comma, semicolon or whatever. Use LISTAGG for this.
I'm designing a web form to publish to a SharePoint webpage in Microsoft Access 2010. I have a form that uses a combo box to select a team name. I need to enter the team id corresponding to that team name into the employee table. This is what I have so far:
INSERT INTO Employee ( Employee_Name, Team_ID )
VALUES ([Forms]![Add Employee]![txtName], (SELECT MAX(Team.Team_ID)
FROM Team, Employee
WHERE [Team]![Team_Name]=[Forms]![Add Employee]![cmbxTeam]));
It gives me an error saying
Query input must contain at least one table or query.
How do I fix this?
You can either use the VALUES clause to specify hard coded values, or you can use a SELECT statement to generate the stuff that will be inserted. Here you are combining the two. Instead:
INSERT INTO Employee ( Employee_Name, Team_ID )
SELECT
[Forms]![Add Employee]![txtName],
MAX(Team.Team_ID)
FROM Team, Employee
WHERE [Team]![Team_Name]=[Forms]![Add Employee]![cmbxTeam];
Slightly unrelated, why are you cross joining employees and teams, when you don't need the Employee table in your SELECT?
This should be equivalent and much much faster:
INSERT INTO Employee ( Employee_Name, Team_ID )
SELECT
[Forms]![Add Employee]![txtName],
MAX(Team.Team_ID)
FROM Team
WHERE [Team]![Team_Name] = [Forms]![Add Employee]![cmbxTeam];
I'm a beginner Programming student and nothing in my text book is helping me with this problem! I need to display these field names ProjectID, ProjectName, DepartmentPhone, EmployeeNumber, LastName, FirstName, EmployeePhone and also the field name Department.
The last field name is included in two separate tables so I used the WHERE clause to make it display.
However, I have to specifically display employees working on projects assigned by the 'Marketing' Department.
I can't get this to work... If I remove my current WHERE clause than Department will not work or display... But I think I need another Where clause to make it only display projects by the Marketing Department.
Here is my current code:
SELECT ProjectID, ProjectName, DepartmentPhone, EmployeeNumber, LastName, FirstName, EmployeePhone
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE PROJECT.Department = EMPLOYEE.Department
AND ProjectID IN
(SELECT PROJECT ID
FROM PROJECT
WHERE Department = 'Marketing')
ORDER BY ProjectID
I'm running it in Access as an SQL Query and the error that pops up says
Syntax error in query expression 'PROJECT.Department = EMPLOYEE.Department AND SELECT ProjectID, ProjectName, DepartmentPhone, EmployeeNumber, LastName, FirstName, EmployeePhone
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE PROJECT.Department = EMPLOYEE.Department
AND ProjectID IN
(SELECT PROJECT ID
FROM PROJECT
WHERE Department = 'Marketing')
ORDER BY ProjectID "
Without "AND" and everything past that it works just fine but displays all projects and not just those run by the Marketing Department. Please help!
When you use IN with select IN (SELECT you have to use exact one column, but I can see 2. Obviously you mean IN (SELECT ID FROM PROJECT...
P.S and yes,- you miss some joining...