average salary for graduating student based on different major - sql

I want to get the average salary for graduating student based on different major
3 tables involve:
Survey_result (Student_Id, Annual_Salary)
Student_Degree(Student_ID, Degree_ID)
Degree(Degree_ID,Major_Name)
Survey_Result
9320000000, $1000
9320000001, $2000
9320000002, $3000
9320000003, $4000
Student_Degree
9320000000, 1
9320000001, 2
9320000002, 3
9320000003, 4
Degree
1, Accounting
2, Finance
3, Accounting
4, Finance
need sql result: Account: 2000, Finance: 3000

I think you can do this:
SELECT
AVG(Survey_Result.Annual_Salary),
Degree.Major_Name
FROM
Degree
JOIN Student_Degree
ON Student_Degree.Degree_ID=Degree.Degree_ID
JOIN Survey_Result
ON Student_Degree.Student_ID=Survey_Result.Student_ID
GROUP BY
Degree.Major_Name

Related

How to get the values corresponding to another table?

I'm new to SQL and am a bit confused on how I would write a query in order to get the count of state in a different table.
Ie i have this table [student]
id
school_code
0
0123
1
2345
2
2345
And this other table [school]
school_code
name
State
0123
xxyy
New Jersey
2345
xyxy
Washington
3456
yxyx
Colarado
I want to find out how I would get this table which tells me the entries for state by checking each student and making a count of how often that state occurs, ordered by most occurrences in student table.
State
No. times occured (iterating through student)
Washington
2
New Jersey
1
SELECT school.state, count(school.state)
FROM student, school
WHERE student.school_code = school.school_code
GROUP BY school.state
ORDER BY count(school.state)`
I'm not sure whether this would be iterating through each student and counting them?
Or just natural-joinging student and school and then counting all the states
When I run this on data supplied, the numbers of times occurred is a really low number which doesn't seem right?
We can simply JOIN the two tables and COUNT the school code in the students table, with GROUP BY state:
SELECT
sc.state, COUNT(st.school_code)
FROM
school sc
JOIN student st
ON sc.school_code = st.school_code
GROUP BY sc.state;
We can try out here: db<>fiddle

How to count same values in different cell as 1 instead of increase by 1 by 1 sql

I'm trying to count the Total Number of Supervisors for each branch according to branch ID (B001, B002, B003). I'm trying to get the result like 'Sandy' counted as 1 and 'Mandy' counted as another (total 2 supervisors for B001) but after i executed, the result showed 3 (Sandy counted separately as 2 different values instead of 1). so, what should i do to make total number of supervisor in B001 branch becomes 2.
there are 3 B001 in that table, but the supervisors are only sandy and mandy which Supervisor Sandy is repeated. The result showed there are 3 supervisors after i executed, so how can i make it to 2?)
results shown:
2
SELECT Staff.BranchID,Branch.Manager AS ManagerName,
COUNT (staff.Supervisor) AS TotalNumberofSupervisor
FROM Staff INNER JOIN Branch ON Branch.BranchID = Staff.BranchID
GROUP BY Staff.BranchID,Branch.Manager
I think "COUNT (distinct(staff.Supervisor))" instead of
"COUNT (staff.Supervisor)" will help

Order enrollment records by date per student

I essentially have a table in MS Access that has student enrollment records like this:
Student ID, Enrollment Date, Enrollment Code
12345, 8/25/2014, E01
12345, 9/5/2014, WD02
12345, 10/3/2014, E01
23456, 8/25/2014, E01
34567, 8/25/2014, E01
34567, 10/01/2014, WD03
The above basically would mean that student 12345 enrolled on 8/25, withdrew on 9/5, and re-enrolled on 10/3; Student 23456 enrolled on 8/25 and is still enrolled; Student 34567 enrolled on 8/25, withdrew on 10/1 and is still withdrawn.
I need to check the order of these records and make sure that we don't have two enrollment records without a withdraw in between and other similar logical errors, as that clearly doesn't make sense.
Here's the issue: I can't figure out for the life of me how to rank these records in Access! Here's what I would like to end up with:
Student ID, Enrollment Date, Enrollment Code, Rank
12345, 8/25/2014, E01, 1
12345, 9/5/2014, WD02, 2
12345, 10/3/2014, E01, 3
23456, 8/25/2014, E01, 1
34567, 8/25/2014, E01, 1
34567, 10/01/2014, WD03, 2
So the rank should start over at every student. This way I can check that every record with a rank of an odd number is an E01 (since that is the only valid entry code) and that each even number is like "WD*", etc. It's not that hard to check right now because as of October we don't have that much movement, but as kiddos start transferring and coming in and out this starts to take hours to look at if you need to look at every student that has more than one record (what I'm currently doing).
Any help would be greatly appreciated. The eventual goal is to automate this in a macro so that it just spits out any crazy records each week and we just fix and move on without having to review every kid that moved.
Try this: You may have to change the field names/table name.
SELECT s.StudentID, s.EnrollmentDate, s.enrollmentcode,
(
SELECT Count(*)
FROM students AS t
WHERE t.enrollmentdate < students.enrollmentdate
AND s.studentid = t.studentid
)+1 AS Test
FROM students AS s
ORDER BY s.StudentID, s.EnrollmentDate;
We basically just use the Enrollment Date as a means to order the records to help us build out the row number. (I don't think anyone can withdraw before they enroll.)

Optional joins in a query (... or ignoring a join if the main table field is null)

I have budgets similar to example below, where:
some budgets are person specific,
some are for the entire team, and
a mixture where some are part allocated to a specific person while the rest is for the rest of the team.
I can link Budgets to Summary_of_spend, but what I need to do is always link on year and budget type, but only on the person if one exists in the budget. Where there is no person in the budget, just default to sum together the amounts for that group.
How would I create a join that does for that?
(Sorry, not come across anything like this before and not sure what it's call, let alone how to solve it. I'm using SQLite by the way.)
Budgets
YEAR, TYPE, PERSON, AMOUNT
2010, Sales, Bob, 1000
2010, Sales, John, 1000
2010, Sales, Fred, 1000
2010, Marketing, Null, 5000
2010, Special, Null, 3000
2010, Special, Bob, 1000
2011, Sales_budget, Bob, 1100
2011, Sales_budget, John, 1100
2011, Sales_budget, Fred, 1100
2011, Marketing, Null, 6000
2010, Special, Null, 3000
2010, Special, Bob, 1000
Spend
DATE, BUDGET, PERSON, AMOUNT
01/01/2010, Sales, Bob, 50
02/01/2010, Marketing, Bob, 100
...
Summary_of_spend
YEAR, BUDGET, PERSON, AMOUNT
2010, Sales, Bob, 950
2010, Sales, John, 888
2010, Marketing, Bob, 500
2010, Marketing, John, 500
2010, Marketing, Fred, 500
2010, Special, Bob, 333
2010, Special, John, 222
2010, Special, Fred, 222
...
Budget_and_summary_of_spend
YEAR, BUDGET_TYPE, PERSON, BUDGET_AMOUNT, SPEND_AMOUNT
2010, Sales, Bob, 1000, 950
2010, Sales, John, 1000, 888
2010, Sales, Fred, 1000, 0
2010, Marketing, Null, 5000, 1500
2010, Special, Null, 3000, 444
2010, Special, Bob, 1000, 333
...
Look into the different types of JOINs: LEFT OUTER JOIN is probably what you need. A left join contains at least one row for each row in the left table; if no match exists in the right table, all its fields are set to NULL. If one or more matches exist, it behaves exactly like an inner join.
Since the SUM function ignores NULL values, grouping and summing the results will work as expected.
You might want to try this:
from budget inner join summary_of_spend
on budget.Year = summary_of_spend.Year
and budget.type = summary_of_spend.Budget
and ifnull(budget.person, 'ItsDepartment') = ifnull(summary_of_spend.Person, 'ItsDepartment')
You can join tables based on any expression. In this case I joined them in such a way that if person is not null the other side must have record for this person and if it is null I substitute some string that will not be found among persons. This is of course weak spot, and if it bothers you you can change join to a combination of and's and or's.
If you need records from budget where no money has been spent, change "inner join" to "left join".
Thanks to tdammers and Nikola M for responding. However, in the end I decided to do a union ... something along the lines of:
Select all the records where there is a budget and a person
UNION ALL
Select all the records where there is only a budget and no specific person
UNION ALL
Select all the remaining records
... and this worked for me.

I need to get the data from the multiple tables and I want to display that in the report form without repeating the primary key values

empID Hobbies Salary
1 Cricket 100
1 Walleyball 100
1 Golf 100
2 Cricket 200
2 Golf 200
I need to get the data from the multiple tables and I want to display that in the report form without repeating the primary key values.. in the above table it should not display empID for every hobby and I need to have the total of salary at the end of the report.
How to overcome such problem
If I understand what you are asking, this is not something you do in SQl, it is something you do in grouping in the report builder.
You are looking for something like this?:
empID Hobbies Salary
1 Cricket
Walleyball
Golf 300
2 Cricket
Golf 400
In five rows like that, or two rows like this:
empID Hobbies Salary
1 Cricket, Walleyball, Golf 300
2 Cricket, Golf 400
And which database system?
SELECT `empID`, SUM(`Salary`) AS `total_salary` FROM yourtable GROUP BY `empID`;
http://www.sql-tutorial.com/sql-group-by-sql-tutorial/
http://www.sql-tutorial.com/sql-aggregate-functions-sql-tutorial/