I'm having some doubts regarding these answers of mine, would greatly appreciate if you guys can clarify if im right or wrong,
Questions:
An employee may be assigned to more than one project and a project may have many employees. Consider the following relational schema and write SQL statements for the below queries.
Employees (empID, empName, empDOB, empAddress, salary, deptID, jobID)
Assignments (empID, projID, assignedDate, completionDate, status)
Projects (projID, projDescription, startDate, endDate, projType)
(a) Display the names of employees who were born before 31st Jan 1980 and assigned a ‘Office Complex’ type project, sort results in ascending order of name. (5 marks)
(b) Retrieve the empIDs who are assigned at least two (2) projects. (5 marks)
Answers:
(a) SELECT empName FROM Employees WHERE empDOB < '31-01-1980' AND projType = (SELECT projType FROM Projects WHERE projType = 'Office Complex') ORDER BY empName;
(b) SELECT empID FROM Employees GROUP BY (SELECT projID From Projects) HAVING COUNT(*)>1 ORDER BY empID;
I feel the answer for the second question may be wrong.
For part a), I'd join the tables to match up employees with the project type:
SELECT empName
FROM Employees
INNER JOIN Assignments ON Assignments.empID = Employees.empID
INNER JOIN Projects ON Assignments.projID = Projects.projID
WHERE empDOB < 31-01-1980 AND projType = 'Office Complex'
ORDER BY empName;
As it stands, your statement attempts to find projType = 'Office Complex' in the Employee table, where it doesn't exist.
For the second question, everything you need is in the Assignments table:
SELECT empID, COUNT(projID)
FROM Assignments
GROUP BY empID
HAVING COUNT(projID) > 1
Related
The employees table
The dept_emp table
SELECT
COUNT(de.emp_no) AS num_of_employees,
e.gender AS gender,
YEAR(de.from_date) AS year
FROM employees e
JOIN dept_emp de
USING (emp_no)
GROUP BY gender, year
ORDER BY year
Using this code, I am able to get the following query results
Failed Results
What I solved here is being able to get the hired number of employees in each year sorted by its gender.
The question I am trying to solve is:
How many males and females have worked throughout the company, showing a running total in each year of active employees during that year.
The problem however, is that I am unable to get the currently active working employees in each year and gender on a running total, since I am using the from_date column and therefore am not taking into account on people leaving the job/unemployed from the to_date column in the dept_emp table.
I spent 10 hours straight trying to figure this out yesterday and half of the day today. Some other codes I used are here, but no luck. Putting this one out here in case maybe there is something I missed etc.
SET #total := 0;
SELECT
gender,
year,
hired_employees,
#total := #total + hired_employees AS currently_employed
FROM
(SELECT
COUNT(de.emp_no) AS hired_employees,
e.gender AS gender,
YEAR(de.from_date) AS year
FROM dept_emp de
JOIN employees e
USING (emp_no)
WHERE gender = "M"
GROUP BY gender, year
ORDER BY gender, year) AS male_employees
failed running total
I get the running total here, in this case for all male employees, but cannot add AND gender = "F" in the where clause, as the running total aren't separate from each gender. Still no luck in sorting out active vs inactive employees per year in this code as well.
Any help would be so much greatly appreciated.
I think that something that might help would be making a calendar table and then joining against that to do your grouping. Assuming you have a calendar table, I think a query like this would get you what you are looking for
SELECT cal.y AS `year`
, SUM(IF(e.gender = 'M', 1, 0)) AS male_count
, SUM(IF(e.gender = 'F', 1, 0)) AS female_count
FROM reference.calendar cal
LEFT JOIN dept_emp de ON (cal.y > YEAR(de.from_date) AND cal.y < YEAR(de.to_date))
LEFT JOIN employees e ON e.emp_no = dept_emp.emp_no
WHERE cal.y BETWEEN [your desired range]
GROUP BY cal.y
Here are some good instructions for creating a robust calendar table.
I am using mySQL to query a mock university database. This is for a class so. The query I am trying to make is to this question:
Find the names and ids of the students who have taken exactly one
course in the Spring 2010 semester.
schema tables:
student(id, name, dept_name, total_cred)
takes(id, course_id, sec_id, semester, year, grade)
I can query the schema for all students who took classes in Spring of 2010 no problem. But where I run into trouble is the 'exactly one class' part.
select distinct s.id, name
from student s join takes
where semester = 'Spring' and
year = 2010;
I thought I would use a set operator like not in to compare that result to another that returns the number of classes taken by each student:
select distinct count(s.id) num_classes, name
from student s join takes
where semester = 'Spring' and
year = 2010
group by name
The problem is that when I run this query it returns the count of 8 for each name. But I have no idea where it is getting that number because there is nothing that occurs exactly 8 times.
My question(s):
1) am I going about this the right way?
2) If so what am I doing wrong to make the count return that way?
Try this:
select s.id, s.name
from student s join takes t on s.id = t.id
where t.semester = 'Spring' and
t.year = 2010
group by s.id, s.name
having count(*) = 1
Our school's got a few extracurricular activities as popular as the one I am running and since it's got hundreds of members, but only a few mentors, we have a strict attendance policy that gets members kicked out after their third unexcused absence.
The PHP-JS-MySQL website I created for the purpose of running the activity stores people's attendance to meetings in this MySQL table:
meetings_attendance (meetings_id, members_id, attendance, excuse)
In this table, meetings_id and members_id are indices referring to the int primary keys in tables covering, obviously, meetings and members. attendance is an enum whose values can be "Pending","Present","Absent" or "Excused", and excuse is a varchar(300) which can be left null in case the member is present.
Since I'm running the program myself, I do not necessarily wish to include the kicking-out functionality in the website (I'd like to do it manually, contacting each member before they are kicked out and the status value in their row of the members table is set to "Inactive"). I'm trying to get a list of the members' names, email address and phone number, from the members table:
members (`id`, `firstname`, `lastname`, `email`, `phone`........)
I have been trying various combinations of MySQL queries to fetch a list of members' absence counts, to no avail. I've tried things like the following, and I have a feeling I should be using DISTINCT somewhere, but I can't pinpoint exactly where or how.
SELECT
firstname,
lastname,
email,
phone,
absences
FROM
meetings_attendance,
members
WHERE
absences = (
SELECT COUNT(*)
FROM meetings_attendance
WHERE attendance = "Absent"
)
AND members.id = meetings_attendance.members_id
Count the absences per member and keep those with a count >= 3 (using GROUP BY and HAVING). Then join the thus found members with the member table and show the results:
select
m.firstname,
m.lastname,
m.email,
m.phone,
a.absences
from members m
join
(
select members_id, count(*) as absences
from meetings_attendance
where attendance = "Absent"
group by members_id
having count(*) >= 3
) a on a.members_id = m.id;
SELECT firstname, lastname, email, phone, absences FROM members
WHERE id IN
(SELECT members_id FROM
(SELECT members_id, COUNT(*) FROM meetings_attendance
WHERE attendance = "Absent"
GROUP BY members_id
HAVING COUNT(*) >= 3 ) AS tab1)
try to format this code below and tell me whether it solve your problem
SELECT *,(SELECT COUNT(*) FROM table2 WHERE table2.field1 = table1.id) AS count FROM table1 WHERE table1.field1 = 'value'
I'm finding it a bit confusing to solve the following questions...
"
An employee may be assigned to more than one project and a project may have many employees. Consider the following relational schema and write SQL statements for the below queries.
Employees (empID, empName, empDOB, empAddress, salary, deptID, jobID)
Assignments (empID, projID, assignedDate, completionDate, status)`
Projects (projID, projDescription, startDate, endDate, projType)
(a) Display the names of employees who were born before 31st Jan 1980 and assigned a ‘Office Complex’ type project, sort results in ascending order of name.
(b) Retrieve the empIDs who are assigned at least two (2) projects.
"
My answer so far .
(a) SELECT empName FROM EMPLOYEES WHERE empDOB < '31-january-1980' AND ....
"
Please help me out
try this :
A)
SELECT employee.empName
FROM Assignments ass
JOIN Employees employee
ON employee .empID= ass.empID
JOIN Projects project
ON project.projID= ass .projID
WHERE employee.empDOB < '31-january-1980'
AND project.projType = ‘Office Complex’
ORDER BY employee.empName;
Without using JOIN CLAUSE
SELECT DISTINCT employee.empName
FROM Employees employee
WHERE employee.empDOB < '31-january-1980'
AND employee.empID IN (
SELECT ass.empID
FROM Assignments ass
WHERE ass.projID IN (
SELECT project.projID
FROM Projects project
WHERE project.projType = ‘Office Complex’
)
)
ORDER BY employee.empName ASC;
b)
SELECT ass.empID, COUNT(*) as counting
FROM Assignments ass
GROUP BY ass.empID
HAVING COUNT(*) >= 2;
I have a table that tracks attendance in a course. The columns are the courseid, lesson, personid, and date. I have a query (below) that extracts the earliest date a person appears along with the associated course, lesson, and personid. This is used to determine when a person started a particular course and ensure they started with the first lesson. This works fine, but where I am stuck is running this query per course. For example, finding the first date each person in a particular course started it rather than for every course. Right now I am just running the more general query and filtering it in the biz layer.
I obfuscated this a bit so forgive any typos:
select a.courseid,
a.lesson,
a.personid,
a.thedate
from (select personid,
min(thedate) as earliestdate
from attendance
group by personid) as x
inner join attendance as a on (a.personid = x.personid and a.thedate = x.thedate)
Just group over person_id, course in the inner query:
select a.courseid, a.lesson, a.personid, a.thedate
from (
select personid, courseid, min(thedate) as earliestdate
from attendance
group by personid, courseid
) as x
inner join attendance as a
on (a.personid = x.personid and
a.thedate = x.thedate and
a.courseid=x.course_id)
I have a small doubt here. Currently all the information is maintained in single data object/table. How would it be if we have different data model like below...?
Objext1:
Course table: Course details having lession with a relation
Student table: Contains student details.
Will the querying would be simplified in this way....?
Sorry if anything sounds immatur...
Regards,
UDAY