SQL Query - Count() and Inner Join - sql

I want to use an inner join to list the student ID, name, and total number of timetabled hours the student has per week, when the student has more than 4 hours per week.
I have three tables required here, student, studentReg and roomBooking as follows
student
id | fname | surname | courseCode
studentReg
sID | modCode
roomBooking
bookingID | roomCode | moduleCode | dayReq | timeReq | semester | classSize
The SQL query I have so far is
SELECT COUNT(moduleCode) AS [Lecture Hours],
id, fname, surname
FROM (student INNER JOIN studentReg ON student.id = studentReg.sID
INNER JOIN roomBooking ON studentReg.modCode = roomBooking.moduleCode)
HAVING COUNT (moduleCode) > 4;
and when I try to run this, I get "syntax error in expression"
Can anyone help me as to what the problem is?

Never sure with nested join in ms access, but I would try something like that
SELECT COUNT(moduleCode) AS [Lecture Hours],
id, fname, surname
FROM student
INNER JOIN (studentReg
INNER JOIN roomBooking ON studentReg.modCode = roomBooking.moduleCode)
ON student.id = studentReg.sID
GROUP BY id, fname, surname
HAVING COUNT (moduleCode) > 4
or maybe
SELECT COUNT(moduleCode) AS [Lecture Hours],
id, fname, surname
FROM (student INNER JOIN studentReg ON student.id = studentReg.sID)
INNER JOIN roomBooking ON studentReg.modCode = roomBooking.moduleCode
GROUP BY id, fname, surname
HAVING COUNT (moduleCode) > 4;

Related

Max function returning multiple values [SQL]

I have 3 tables: money, student, faculty. This query returns each faculty and highest stipend in each one of them.
select
f.name as "FACULTY_NAME",
max(stipend) as "MAX_STIPEND"
from
money m, student s
inner join
faculty f on f.id_faculty = s.faculty_id
where
m.student_id = s.id_student
group by
f.id_faculty, f.name;
Query works fine:
FACULTY_NAME | MAX_STIPEND
-----------------+---------------
IT Faculty | 50
Architecture | 60
Journalism | 40
However when I add s.name to original query to also show the name of the student who received max_stipend, query is not working like it used to - it returns all of the students
select
f.name as "FACULTY_NAME",s.name,
max(stipend) as "MAX_STIPEND"
from
money m, student s
inner join
faculty f on f.id_faculty = s.faculty_id
where
m.student_id = s.id_student
group by
f.id_faculty, f.name, s.name;
Query result:
FACULTY_NAME | s.name | MAX_STIPEND
----------------+-----------+---------------
IT Faculty | Joe | 50
IT Faculty | Lisa | 10
Architecture | Bob | 60
Journalism | Fred | 5
Architecture | Susan | 5
Journalism | Tom | 40
It does the same thing using right, left and inner joins. Can someone tell where the problem is?
First, you should be using proper JOIN syntax for all your joins.
Second, you can use Oracle's keep syntax:
select f.name as FACULTY_NAME,
max(stipend) as MAX_STIPEND,
max(s.name) keep (dense_rank first order by stipend desc)
from money m join
student s
on m.student_id = s.id_student join
faculty f
on f.id_faculty = s.faculty_id
group by f.id_faculty, f.name;
However when I add s.name to original query to also show the name of the student who received max_stipend, query is not working like it used to - it returns all of the students
When you add s.name you are looking for min value for each user.
If you want the name of user who has the MAX_STIPEND you should to move to window functions. For example Dense Rank in MS SQL Server.
with cte as
(select
f.name as "FACULTY_NAME",
s.name as "STUDENT_NAME",
stipend as "MAX_STIPEND",
DENSE_RANK() OVER
(PARTITION BY f.name, s.name ORDER BY i.stipend DESC) AS Rank
from
money m
inner join student s on m.student_id = s.id_student
inner join
faculty f on f.id_faculty = s.faculty_id
)
select "FACULTY_NAME", "STUDENT_NAME"
from cte
where rank = 1
Not all sql brands have windowed functions. Here the link for dense_rank on MySQL and also dense_Rank for Oracle

SQL inner joins on three tables

I have three tables that needs to be checked in order to find out on which courses professor is active.
table_teacher
table_course; and
table_teacher_holds_course
table_teacher looks like this:
username | title
---------+----------
john | professor
mark | assistant
table_course looks like this:
course_code | course_name | semester | school_year
-------------+-------------+----------+------------
course_code1| course1 |semester1 | 2015
course_code2| course2 |semester2 | 2015
course_code3| course3 |semester3 | 2015
table_teacher_holds_course looks like this:
username | course_code
---------+-------------
john |course_code1
mark |course_code2
and when I have professors username when he logs on the page, I would like to do left inner join on these three tables in order to show professors courses from table_course
Can someone help me with this, because it is first time to me to use sql to join search in several tables.
Join on the tables common fields
SELECT *
FROM table_teacher t
INNER JOIN table_teacher_holds_course hc ON t.username = hc.username
INNER JOIN table_course c ON hc.course_code = c.course_code
Relevant example:
SELECT t.title, t.username, c.course_code, c.course_name, c.semester, c.school_year
FROM table_teacher t
INNER JOIN table_teacher_holds_course hc ON t.username = hc.username
INNER JOIN table_course c ON hc.course_code = c.course_code
WHERE t.username = 'John'
Result:
title username course_code course_name semester school_year
professor John course_code1 course1 semester1 2015

select only one row that has the highest count in sql

I need to select one row only which has the highest count. How do I do that?
This is my current code:
select firstname, lastname, count(*) as total
from trans
join work
on trans.workid = work.workid
join artist
on work.artistid = artist.artistid
where datesold is not null
group by firstname, lastname;
Example current:
FIRSTNAME | LASTNAME | TOTAL
------------------------------
Tom | Cruise | 3
Angelina | Jolie | 9
Britney | Spears | 5
Ellie | Goulding | 4
I need it to select only this:
FIRSTNAME | LASTNAME | TOTAL
--------------------------------
Angelina | Jolie | 9
You can add order by total desc and fetch first 1 row only (since Oracle 12c r1 only, otherwise you should use your result as temp table and select from it to use rownum = 1 limitation in the where clause) , in case you total can't be the same for different groups. The other way is to add this having clause, so you can list all people with maximum total:
having count(*) = (select max(total) from (select count(*) as total from <your_query>) tmp)
or that:
having count(*) = (select count(*) as total from <your_query> order by total desc fetch first 1 row only)
In Oracle 12, you can do:
select firstname, lastname, count(*) as total
from trans join
work
on trans.workid = work.workid join
artist
on work.artistid = artist.artistid
where datesold is not null
group by firstname, lastname
order by count(*) desc
fetch first 1 row only;
In older versions, you can do this with a subquery:
select twa.*
from (select firstname, lastname, count(*) as total
from trans join
work
on trans.workid = work.workid join
artist
on work.artistid = artist.artistid
where datesold is not null
group by firstname, lastname
order by count(*) desc
) twa
where rownum = 1;
this will work sql server 2012..
with CTECount (firstname, lastname,total)
as
(
select firstname, lastname, count(1) as total
from trans
join work
on trans.workid = work.workid
join artist
on work.artistid = artist.artistid
where datesold is not null
group by firstname, lastname
)
select top(1) with ties from CTECount
order by total desc
Thanks
You could do like this, very simple:
select TOP 1 firstname, lastname, count(*) as total from trans
join work on trans.workid=work.workid
join artist on work.artistid=artist.artistid
where datesold is not null
group by firstname, lastname
Order By Total DESC;

Group Join between three tables to get the percentage

I have three tables Attendance, Employee, Sector
Employee Table
EmiId -Name -SectorId
123 ABC 1
231 BCD 2
125 WER 1
Attendance
AttId -EmpId -Dt
1 123 12/12/2014 9:00
2 231 12/12/2014 10:00
Sector
SectorId -SectorName
1 North Sector
2 East Sector
my query is
SELECT COUNT(Attendance.Emp_Id) as AttCount,(select COUNT(*) from Employee) as EmpCount
FROM Employee INNER JOIN
Sector ON Employee.SectorId = Sector.SectorId INNER JOIN
Attendance ON Employee.EmpId = Attendance.EmpId
group by Sector.SectorId
and i keep getting same number of employees for both instead so the (select COUNT(*) from Employee)- EmpCount seems to be incorrect.I keep getting the same number for both the sectors. Although the Attcount seems to work fine.
Please help. Thanks in advance.
You just making select count from the same table - do not expect other results.
Perhaps someone could do it better
SELECT COUNT(Attendance.Emp_Id),COUNT(case when Employee.empid=attendance.attid then 1 else 0 end)
FROM Employee JOIN Sector ON Employee.SectorId = Sector.SectorId
LEFT JOIN Attendance ON Employee.EmpId = Attendance.Emp_Id
group by Sector.SectorId
Maybe you need LEFT JOIN with Attendance
SELECT COUNT(Attendance.Emp_Id),(select COUNT(*) from Employee)
FROM Employee JOIN Sector ON Employee.SectorId = Sector.SectorId
LEFT JOIN Attendance ON Employee.EmpId = Attendance.EmpId
group by Sector.SectorId

How to count occurrences of a column value in SQL?

I have a table of students:
id | age|num
-------------
0 | 25| 10
1 | 25| 5
2 | 23| 5
I want to query for all students, and an additional column that counts how many students are of the same age:
id | num | age | agecount|numcount
-------------------------------------
0 | 10 | 25 | 2 |1
1 | 5 | 23 | 1 |2
What's the most efficient way of doing this? if there's a better way**. Is there?
You have two queries:
One for the list of the students:
SELECT
id, age, num
FROM
students
And one for the count of students with the same age:
SELECT
age
, count(1)
FROM
students
GROUP BY
age
Now you have to combine these two queries:
You can JOIN one or more tables or subqueries. Lets do it:
SELECT
S.id, S.age, S.num, age.cnt
FROM
-- List of all students
(
SELECT
id, age, num
FROM
students
) S
-- Ages with student counts
INNER JOIN (
SELECT
age
, count(1) AS cnt
FROM
students
GROUP BY
age
) A
ON S.age = A.age
You can simplify the above query with removing the first subquery and use the students table instead:
SELECT
S.id, S.age, S.num, A.cnt
FROM
students S
-- Ages with student counts
INNER JOIN (
SELECT
age
, count(1) AS cnt
FROM
students
GROUP BY
age
) A
ON students.age = age.age
Now you can modify this sample query to achieve your goal.
To count student with the same age :
select age ,count(age) from s_table group by age