Microsoft SQL server select statements on multiple tables? - sql

so I've been struggling with some of the select statements on multiple tables:
Employee table
Employee_ID
First_Name
Last_Name
Assignment table
Assignment_ID
Employee_ID
Host_Country_arrival_Date
Host_Country_departure_Date
Scheduled_End_Date
I'm being asked to display query to display employee full name, number of days between the host country arrival date and host country departure date, number of days between today's date and the assignment scheduled end date and the results sorted according to host country arrival date with the oldest date on top.
also, I'm not familiar with the sort function in SQL server..
Here's my query and I've been getting syntax errors:
SELECT
First_Name
Last_Name
FROM Employee
SELECT
Host_Country_Arrival_Date
Host_Country_Departure_Date
FROM Assignment;

So, Basically what your code is doing is 2 different queries. The first getting all the employees names, and the second one getting the dates of the assignments.
What you'll want to do here is take advantage of the relationship between the tables using a JOIN. That is basically saying "Give me all employees and all of HIS/HERS assignments". So, for each assignment that the employee has, it will bring a row in the result with his name and the assignment info.
To get the difference between days you use DATEDIFF passing 3 parameters, the timespan in which to calculate the difference, the first and the second date. It will then Subtract the first one from the second one and give you the result in the selected timespan.
And finnaly the sorting: Just add 'ORDER BY' followed by each column that you want to use for ordering and then specify if you want it ascending (ASC) or descending (DESC).
You can check how I would answer the if that question was proposed to me in a coding challenge.
SELECT
CONCAT(E.First_Name,' ', E.Last_Name) FullName,
DATEDIFF(DAY,Scheduled_End_Date,getdate()) DaysTillScheduledDate,
DATEDIFF(DAY,Host_Country_Arrival_Date,Host_Country_Departure_Date) DaysTillScheduledDate
FROM Employee As E --Is nice to add aliases
Inner Join
Assignment As A
on E.Employee_ID = A.Employee_ID -- Read a little bit about joins, there are a lot of material availabel an its going to be really necessary moving forward with SQL
order by Host_Country_Arrival_Date DESC -- Just put the field that you want to order by here, desc indicates that it should be descending

You should use a JOIN to link the tables together on Employee_ID:
SELECT
First_Name,
Last_Name,
Host_Country_Arrival_Date,
Host_Country_Departure_Date
FROM Employee
JOIN Assignment ON Assignment.Employee_ID = Employee.Employee_ID;
What this is saying basically is that for each employee, go out to the assignments table and add the assignments for that employee. If there are multiple assignments, the employee columns will be repeated on each row with the assignment columns for the assignment.

You need to look for the join and group by. Please find this link for reference tutorial
For now you may try this...
SELECT
CONCAT(Emp.First_Name,' ', Emp.Last_Name) FullName,
DATEDIFF(DAY,Scheduled_End_Date,getdate()) DaysTillScheduledDate,
DATEDIFF(DAY,Host_Country_Arrival_Date,Host_Country_Departure_Date) DaysTillScheduledDate
FROM Employee As Emp Inner Join Assignment As Assign on Emp.Employee_ID = Assign.Employee_ID
order by Host_Country_Arrival_Date DESC

Related

How To Select data from multiple tables with grouping for duplicates

I have Two Tables, one with Employees Details and another with vacations taken by them in different years.Please check this image for the tables
Here as you'll find out in the vacation table, for the same employee with same employeeId and in sam year different vacation days are mentioned. Like John Smith in 2011 have two entries one with 10 vacation and one with 3 vacation. I want my query to return a single row with vacations mentioned as 13.
I tried the following query but no luck
SELECT Employee_Details.EmployeeId, Employee_Details.EmployeeName, Employees_Vacation.Year, Employees_Vacation.Vacation, Employee_Details.Department
FROM Employees_Vacation INNER JOIN Employee_Details ON Employees_Vacation.EmployeeId=Employee_Details.EmployeeId group by Employee_Details.EmployeeId ORDER BY Employee_Details.EmployeeName, Employees_Vacation.Year ;
if i understood you right, i think this may help you
select sum(vacation) as sum, ev.year, ed.EmployeeName from employee_Details as ed inner join employee_Vacation as ev
on ed.employeeID = ev.employeeID
group by ev.year, ed.EmployeeName
A lot here will depend on the sql engine you are using, however there are some things that will apply regardless of the engine to consider:
Your current GROUP BY clause is grouping only by employeeId - from the question text it seems like you are instead looking for results grouped by employee AND vacation year
Your projection (SELECT statement) currently isn't actually aggregating anything - it's just projecting a bunch of fields. On some db engines, this actually isn't even allowed (SQL Server for example will only allow grouped or aggregated columns in the projection). Again, from the question text it seems like you are looking for the SUM of vacation days per employee and year.
Taking these into account and assuming the assumptions made are accurate, something like the following should work in most/all modern RDBMS's:
SELECT Employee_Details.EmployeeId,
Employee_Details.EmployeeName,
Employees_Vacation.Year,
SUM(Employees_Vacation.Vacation) AS TotalVacationDays,
Employee_Details.Department
FROM Employees_Vacation
INNER JOIN Employee_Details
ON Employees_Vacation.EmployeeId = Employee_Details.EmployeeId
GROUP BY
Employee_Details.EmployeeId, Employee_Details.EmployeeName,
Employees_Vacation.Year, Employee_Details.Department
ORDER BY
Employee_Details.EmployeeName,
Employee_Details.EmployeeId,
Employees_Vacation.Year;
You may be able to get away with fewer grouping clauses in some engines (MySql for example). Additionally I added an EmployeeId to the order by clause to ensure records for the same employee remain together in the results (for employees with the same names for example).

Extract info from one table based on data from antoher

I am kind of new to SQL and I made a couple of tables to practice. The columns may have some unrelated categories but I don't know what else write...
Anyway, basically what i want to do is get info from two tables based on the first and last name from one table.
Here are my tables:
Order
Host
I want create a query to pull the ticket number, height, order, subtotal and total by first and last name. The only orders I want to pull are from John Smith And Sam Ting. So in the end, I want my extraction to have the following columns:
Ticket Number
First Name
Last Name
Height
Order
Subtotal
Total
Any help or direction would be awesome!
With the assumption the tables both have unique Ticket_Numbers and that will provide a one-to-one mapping between then.
SELECT
Order.Ticket_Number,
First_Name,
Last_Name,
Height,
Order,
Subtotal,
Total
FROM Order
JOIN Host on Host.Ticket_Number = Order.Ticket_Number
WHERE
(First_Name = 'John' AND Last_Name = 'Smith')
OR (First_Name = 'Sam' AND Last_Name = 'Ting')
You need to "call" the table name first, and then the column. After that you need to use the "join" for the 2 tables. And finally you need the "where". I didn't look for the details so you need to check the "names".
SELECT Order.Ticket_Number, Order.First_Name, Order.Last_Name, Order.Height, Order.Order, Cost.Subtotal, Cost.Total
FROM Order
INNER JOIN Cost
where First_Name="Jhon" and Last_Name="blablabla"
or
First_Name="SecondGuy" and Last_Name="blablabla"

How to correct "expression must have same data type as corresponding expression" error?

For example I have table department and employee
I want to find the names of the employees together with budget of department employees belong to
Inside employee table it contain employee name which is ename
department table it contain budget for department
The command that I used
SELECT ENAME FROM EMPLOYEE
UNION
SELECT BUDGET FROM DEPARTMENT
ORDER BY ENAME;
but I keep getting expression must have same data type as corresponding expression error
Can someone explain to me What is wrong with my concept and how to obtain the result .
As the error says. It looks as though ENAME is a string and BUDGET is a numeric.
It sounds like you want to return these two values in a query along the lines of:
SELECT ENAME, SUM(BUDGET)
FROM EMPLOYEE INNER JOIN BUDGET
ON EMPLOYEE.ID = BUDGET.ID
GROUP BY ENAME
Note I'm making assumptions as to your data types, randomly throwing an ID out there, and assuming you want to sum the data. Oh, and the platform. The SQL should be pretty standard I guess, but this is T-SQL.
I guess You're trying to union two tables which are having different datatype, and union requires datatype of both columns should be same.
http://technet.microsoft.com/en-us/library/ms180026.aspx
See the msdn says The data types must be compatible.

SQL select / group

I am a student this is homework. I'm getting tired and confused. Any advice will be appreciated.
I have two tables.
Employee has the following columns:
Last_name
First_name
Address
Phone
Job_title(FK)
Wage
Job_title has
job_title(PK)
EEO classification
Job_description
Exempt_Non_Exempt
I need to select the employees’ last names and group them by salary within job titles that are grouped into exempt and non-exempt.
I'm using sql server to check my work but it needs to be hand scripted.
Can you provide sample data? Because it's not clear to me what the data type for JOB_TITLE.exempt_non_exempt is, or what is to be accomplished by the specified grouping criteria - EMPLOYEE.last_name will be mostly unique (but it can't be guaranteed due to the Mr. Smith principle), so it sounds like there's a need for aggregate function use.
Based on what I've read, this looks to be what you're after:
SELECT e.last_name, e.wage, jt.exempt_non_exempt
FROM EMPLOYEE e
JOIN JOB_TITLE jt ON jt.job_title = e.job_title
GROUP BY e.last_name, e.wage, jt.exempt_non_exempt
You join on the foreign/primary key to get valid data from both tables.
The GROUP BY clause is where you define grouping, but SQL standard is that if you specify columns in the SELECT clause without being wrapped in aggregate functions (IE: COUNT/MAX/MIN/etc), then those columns need to be specified in the GROUP BY.

SQL - Updating records based on most recent date

I am having difficulty updating records within a database based on the most recent date and am looking for some guidance. By the way, I am new to SQL.
As background, I have a windows forms application with SQL Express and am using ADO.NET to interact with the database. The application is designed to enable the user to track employee attendance on various courses that must be attended on a periodic basis (e.g. every 6 months, every year etc.). For example, they can pull back data to see the last time employees attended a given course and also update attendance dates if an employee has recently completed a course.
I have three data tables:
EmployeeDetailsTable - simple list of employees names, email address etc., each with unique ID
CourseDetailsTable - simple list of courses, each with unique ID (e.g. 1, 2, 3 etc.)
AttendanceRecordsTable - has 3 columns { EmployeeID, CourseID, AttendanceDate, Comments }
For any given course, an employee will have an attendance history i.e. if the course needs to be attended each year then they will have one record for as many years as they have been at the company.
What I want to be able to do is to update the 'Comments' field for a given employee and given course based on the most recent attendance date. What is the 'correct' SQL syntax for this?
I have tried many things (like below) but cannot get it to work:
UPDATE AttendanceRecordsTable
SET Comments = #Comments
WHERE AttendanceRecordsTable.EmployeeID = (SELECT EmployeeDetailsTable.EmployeeID FROM EmployeeDetailsTable WHERE (EmployeeDetailsTable.LastName =#ParameterLastName AND EmployeeDetailsTable.FirstName =#ParameterFirstName)
AND AttendanceRecordsTable.CourseID = (SELECT CourseDetailsTable.CourseID FROM CourseDetailsTable WHERE CourseDetailsTable.CourseName =#CourseName))
GROUP BY MAX(AttendanceRecordsTable.LastDate)
After much googling, I discovered that MAX is an aggregate function and so I need to use GROUP BY. I have also tried using the HAVING keyword but without success.
Can anybody point me in the right direction? What is the 'conventional' syntax to update a database record based on the most recent date?
So you want to update the AttendantsRecordsTable, and set the comment to the comment in the most recent CourseDetailsTable for each employee?
UPDATE
dbo.AttendanceRecordsTable
SET
Comments = #Comments
FROM
CourseDetailsTable cd
INNER JOIN
Employee e ON e.EmployeeID = AttendanceRecordTable.EmployeeID
WHERE
e.LastName = #LastName
AND e.FirstName = #FirstName
AND cd.CourseName = #CourseName
AND AttendanceRecordsTable.CourseID = cd.CourseID
AND AttendanceRecordsTable.LastDate =
(SELECT MAX(LastDate)
FROM AttendanceRecordsTable a
WHERE a.EmployeeID = e.EmployeeID
AND a.CourseID = cd.CourseID)
I think something like that should work.
You basically need to do a join between the AttendanceRecordTable, which you want to update, and the Employee and CourseDetailsTable tables. For these two, you have defined certain parameters to select a single row each, and then you need to make sure to update only that last AttendanceRecordTable entry which you do by making sure it's the MAX(LastDate) of the table.
The subselect here:
(SELECT MAX(LastDate)
FROM AttendanceRecordsTable a
WHERE a.EmployeeID = e.EmployeeID AND a.CourseID = cd.CourseID)
will select the MAX (last) of the LastDate entries in AttendanceRecordsTable, based on selection of a given employee (e.EmployeeID) and a given course (cd.CourseID).
Pair that with the selects to select the single employee by first name and last name (that of course only works if you never have two John Miller in your employee table!). You also select the course by means of the course name, so that too must be unique - otherwise you'll get multiple hits in the course table.
Marc
Assuming that you primary key on the AttendanceRecordsTable is id:
UPDATE AttendanceRecordsTable SET Comments = #Comments
WHERE AttendanceRecordsTable.id = (
SELECT AttendanceRecordsTable.id
FROM EmployeeDetailsTable
JOIN AttendanceRecordsTable ON AttendanceRecordsTable.EmployeeID = EmployeeDetailsTable.EmployeeID·
JOIN CourseDetailsTable ON AttendanceRecordsTable.CourseID = CourseDetailsTable.CourseID
WHERE
EmployeeDetailsTable.LastName =#ParameterLastName AND EmployeeDetailsTable.FirstName =#ParameterFirstName AND
CourseDetailsTable.CourseName =#CourseName
ORDER BY AttendanceRecordsTable.LastDate DESC LIMIT 1)
Basically, that sub select will first join the attendence, employee and coursedetail tables, extract those rows where the employee's and course details' name match those given by your parameters and limit the output in reverted order to one line. You might want to test that sub-select statement first.
Edit: I just read your posting again, you don't have a single primary key column on AttendanceRecordsTable. Bummer.