SQL: Using the AND statement with nested queries - sql

I am learning with an online tutorial series and right now we are looking into nested queries. Trying to play around and think of ways I could play with this concept I wanted to try to make combined nested queries, but I am not really sure how and google isn't providing me much luck. Once again it's hard to word things like this. I am using MS SQL
SELECT EmployeeID, FirstName, LastName
FROM SQLTutorial.dbo.EmployeeDemographics
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.EmployeeSalary
WHERE JobTitle = 'DBA')
/*
AND
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.WareHouseEmployeeDemographics
WHERE Age = 29)
*/
This is what I thought I could do. From the example I know that the uncommented part works. It gets the ID, First Name, and Last name from EDemo IF the ID is in BOTH EDemo AND ESalary AND their job title is DBA.
Well I wanted to limit those results further by only having the results be those who work in the Warehouse as well AND are 29.
I mean if I run the commented code without the uncommented it works like intended as well, but I am not sure why I cannot just combine the two. I am 90% sure it's b/c I can't use the AND statement like this and I have a sneaking feeling I have to nest the two together and make a nest in a nest.
And it will be like a taco inside taco within a Taco Bell that's inside a KFC that's within a mall that's inside your dream! Sorry I couldn't help myself.

Just remove second second "Where" and your query is good to execute. You just need one where clause within which you can have all your condition combined with and, or etc..
SELECT EmployeeID, FirstName, LastName
FROM SQLTutorial.dbo.EmployeeDemographics
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.EmployeeSalary
WHERE JobTitle = 'DBA')
AND EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.WareHouseEmployeeDemographics
WHERE Age = 29)
If you want all the employees whose ID is in BOTH EDemo AND ESalary AND their job title is DBA but or who work in the Warehouse AND are 29.
SELECT EmployeeID, FirstName, LastName
FROM SQLTutorial.dbo.EmployeeDemographics
WHERE EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.EmployeeSalary
WHERE JobTitle = 'DBA')
or EmployeeID
IN (SELECT EmployeeID
FROM SQLTutorial.dbo.WareHouseEmployeeDemographics
WHERE Age = 29)

As pointed out in the comments, the issue is having two WHEREs. Only the first WHERE is needed; after specifying WHERE, conditions can just be joined using AND, OR, that sort of thing. Uncommenting and removing the second WHERE should fix it. So, instead of "WHERE a=b AND WHERE c=d" just write "WHERE a=b AND c=d".

Related

Attempting to list employee name and ID for employees working on a property in Seattle

I'm supposed to use a subquery, but I'm not quite certain where I'm going with this.
I know that I need to match EmployeeIDs from the ggemployee table and the property_service table, then match the PropertyID from the property_service and owned_property tables, then make sure that the PropertyID is tied with the property in Seattle.
I'm just a bit lost with where I need to go from here.
This is the code I have now:
select ggemployee.LastName, ggemployee.FirstName, ggemployee.EmployeeID
from ggemployee
inner join property_service on property_service.EmployeeID = ggemployee.EmployeeID
inner join owned_property on property_service.PropertyID = owned_property.PropertyID
and owned_property.PropertyName = 'Seattle';
I attempted to do this with join on syntax, but haven't quite got it working. If someone could show me the proper way to do this with join on and subqueries that would be absolutely amazing.
Thank you so much in advance for the help!
EDIT:
I also tried something like this..
select ggemployee.LastName, ggemployee.FirstName, ggemployee.EmployeeID
from ggemployee, property_service, owned_property
where property_service.EmployeeID = ggemployee.EmployeeID
and property_service.PropertyID = owned_property.PropertyID
and owned_property.PropertyName = 'Seattle';
EDIT2: I am dumb, PropertyName is supposed to be city. Sorry!
PropertyName need be changed to City. That fixes all issues.
Granted the proper way to use subqueries is the following:
select LastName, FirstName, EmployeeID
from ggemployee
where EmployeeID in
(select EmployeeID from property_service where PropertyID in
(select PropertyID from owned_property where city = 'Seattle'));

Access SQL Can't Create Two Grouped Averages

Apologies for my simple problem, I am an absolute novice. I have the following code in separate queries
I am attempting to display 3 columns, the average male salary for a set job, average female salary for a set job and the JobID. Separately these queries work however I cannot work out how to combine them.
I have tried multiple solutions from this site for example trying to put multiple select statements inside
and also by using a 'union' solution however cannot get either to work.union This simply compiles them into a single column and sorts via salary not JobID.
SELECT Round(Avg(Salary)) AS AverageMaleSalary, JobID
FROM Employee WHERE Gender = "M"
GROUP BY JobID;
SELECT Round(Avg(Salary)) AS AverageFemaleSalary, JobID
FROM Employee WHERE Gender = "F"
GROUP BY JobID;
You could use conditional aggregation
SELECT JobId,ROUND(AVG(IIF(Gender='F', Salary, NULL))) AS AverageFemaleSalary
,ROUND(AVG(IIF(Gender='M', Salary, NULL))) AS AverageMaleSalary
FROM Employee
GROUP BY JobId;

Count unique values in Access query

I'm pretty much an Access n00b, but I've built a basic database for a friend's school to keep track of students seeing the counselling team. They're all pretty happy with it, but they've asked if it's possible to quickly see how many students each counselor is seeing.
I have tables set up for staff, students, and case notes, and I thought it would be easy to create a query that does this, but I cant get it to work; it keeps returning the number of case notes per staff member, rather than the students:
Query right now. Staff #7 has seen one student twice.
What I'd like it to do is tell me how many students each counselor is seeing, based on the case notes they've entered.
I'm really hoping for a solution I can implement in design view, rather than SQL.
You can DL the database if you want to look at it:https://drive.google.com/file/d/0B0RvbnEcKEagZldJMDZkZmkybkk/view?usp=sharing
(It's all dummy data)
Thanks in advance for any guidance you can offer me.
I was going to say, "count distinct," but apparently that's not a thing in Access.
Instead, you'll need to run two queries that are the equivalent of
select StaffID, FirstName, LastName, count(*)
from (
select distinct StaffID, FirstName, LastName, Student
from TblStaff stf join TblCaseNote nte on nte.Staff = stf.StaffID
) foo
group by StaffID, FirstName, LastName
The first query should have StaffID, FirstName, LastName, and Student, each set to Group By.
Then create another query that has the first as its source and Group By all columns except Student, which you should count.

Main reason of using "^"

I don't understand what what the purpose of using "^" in the second code?
Another problem is that I don't retrieve right information in second code compare to the first code. Why?
-- First code, use adventurework DW 2008
select FirstName, LastName from DimCustomer
where LastName like '[j-N]%'
order by LastName
-- Second code, use adventurework DW 2008
select FirstName, LastName from DimCustomer
where LastName like '[^L-N]%'
order by LastName
^ means where the character is not within the specified range.
Which will probably explain why you don't get the results you though you would?

Different ways to alias a column

What is the difference between
select empName as EmployeeName from employees
versus
select EmployeeName = empName from employees
from a technical point of view. Not sure if this is just SQL server specific or not.
Appreciate your answers.
I'd prefer the first one, since the second one is not portable -
select EmployeeName = empName from employees
is either a syntax error (at least in SQLite and Oracle), or it might not give you what you expect (comparing two columns EmployeeName and empName and returning the comparison result as a boolean/integer), whereas
select empName EmployeeName from employees
is the same as
select empName as EmployeeName from employees
which is my preferred variant.
The main advantage of the second syntax is that it allows the column aliases to be all lined up which can be of benefit for long expressions.
SELECT foo,
bar,
baz = ROW_NUMBER() OVER (PARTITION BY foo ORDER BY bar)
FROM T
I don't think there's a technical difference. Its mainly preferential. I go for the second as its easier to spot columns in big queries, especially if the query is properly indented.