How to order rows EXACTLY like the image given? - sql

so I have been given an image and you have to create an output from your table via SQL. I managed to display the Employee Names with the assigned Manager Names but the order of the names did not match the question.
I am thinking ORDER BY WHERE may be used?
Do you think this will help because I don't think I'm implying this correctly maybe
https://sqlandme.com/2013/11/18/sql-server-custom-sorting-in-order-by-clause/
REQUIRED:
SELECT CONCAT(CONCAT(CONCAT(CONCAT(m.TITLEOFCOURTESY,' '),m.FIRSTNAME),' '),m.LASTNAME) AS "Manager Name",CONCAT(CONCAT(CONCAT(CONCAT(a.TITLEOFCOURTESY,' '),a.FIRSTNAME),' '),a.LASTNAME) AS "Employee Name"
FROM EMPLOYEES a
INNER JOIN EMPLOYEES m
ON a.REPORTSTO = m.EMPLOYEEID;
MY OUTPUT:
STEVEN should be third but he is actually second.

If you want to order by the employee name you can paste the same condition in the select to the order by clause.
Judging by the expected output it looks like it is also ordered by salary, department or another column you didn't mention in the question.

Related

Why is my "where" statement not working when it's being designated?

I'm currently trying to write an SQL statement which does the following;
"The sales department now uses the abbreviation SL instead of SA. Change the abbreviation in the job description. Select all employees of the sales department except Taylor. Sort by the total."
This is the code i'm currently trying to run, any advice?
SELECT CONCAT("first_name","", "last_name")
FROM employees NOT "Taylor"
WHERE = "sl"
ORDER by verschil DESC
You have not stated which DMBS you are using. But you have a few obvious syntax errors.
You need to specify the column name for the department in the WHERE clause such as
SELECT ... FROM ... WHERE column_name = "sl" ORDER BY...;
I would suggest you also include your logic for excluding the person called "Taylor" inside the WHERE clause.
First of all, I am assuming you're working with postgresql, secondly you're trying to get all employees name from employees table who are from sales department (you need to replace column_name1 with the actual name of the column where 'SL' value is inserted, replace column_name2 by the name of the column by which you want the result to sort on).
select first_name, last_name
from employees
where first_name<>'Taylor' and last_name<>'Taylor' and column_name1='SL'
order by column_name2
This should fix your issue :
SELECT first_name," ",last_name
FROM employees
WHERE = "sl" and NOT "Taylor"
ORDER by verschil DESC

When using JOIN feature in Oracle, I have some question about when I am using extra ALIAS in this code

Before start, Sorry some of results and datas are written in Korean.
Here is a code that I currently am looking on.
SELECT S.*, D.DNAME
FROM STUDENT S, DEPARTMENT D
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
FROM STUDENT GROUP BY SUBSTR(JUMIN,7,1))
AND S.DEPTNO1 = D.DEPTNO;
And here is the DEPARTMENT data.
And this is the STUDENT data.
I got a result as I want. But I have some questions when I change this part of the code
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
into this one
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT
S.SUBSTR(JUMIN,7,1),MAX(S.WEIGHT)
What I do is simply put S infront of JUMIN and WEIGHT in line3.
But when I do this it shows me the whole data.
I thought JOIN ALIAS (which are S,D in this code) is used as way show that I have two tables to use that is labeled with S and D. S means this data is in STUDENT and D is in DEPARTMENT.
But I think I get it in a wrong way.
Anyway I have no idea how this result is come out.
This one is referring to your main table not the subquery table.
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT
S.SUBSTR(JUMIN,7,1),MAX(S.WEIGHT)
Your original query is already correct. In which you already have a new result set based on your aggregation.
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)

Microsoft SQL server select statements on multiple tables?

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

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.