SQL Server Rules of Precedence - sql-server-2008

What is the difference between two queries :
Query #1:
select *
from Employee
where Job_id = 'SA_REP' OR Job_id = 'AD_PRES' AND salary > 15000
Output:
EmpId Last_name Job_ID Salary
1 King AD_PRES 24000
2 Abel SA_REP 11000
3 Taylor SA_REP 8600
4 Grant SA_REP 7000
Query #2:
select *
from Employee
where (Job_id = 'SA_REP' OR Job_id = 'AD_PRES') AND salary > 15000
Output:
EmpId Last_name Job_ID Salary
1 King AD_PRES 24000

In the first query you will get all Employees with a Job_ID of 'SA-REP' no matter what their salary is and Employees with a Job_ID of 'AD_PRES' and salary > 15000. In the second query you will get Employees whose salary > 15000 and also have a Job_ID of 'SA_REP' or 'AD_PRES'.

Related

LIMIT and Group By? How do I return the lowest 100 earning customers by country? SQL

Hi I’ve a table database1
3 columns : customer_id , income , country
Customer_id
1001
1002
...
Income
5000
6000
7000
Country
SG
HK
VN
...
How do I write a query that returns the lowest 100 earning customers per country?
Is it possible to return:
Customer ID | country code
1003 SG
1004 SG
...
1007 VN
...
So on
Thanks!
On mySQL 8 you can leverage a window function for this:
SELECT * FROM
(
SELECT
country,
customer_id,
row_number() over(partition by country order by income asc) earn_rank
FROM table
)x
WHERE x.earn_rank <= 100
You can conceive that this window function will sort the rows by country then by income, then start counting up from 1. Each time the country changes the row numbering starts over from 1 again. This means that for every country there will be a row numbered 1 (with the lowest income), and a 2, 3 etc. If we then wrap it up in another outer query that selects only rows where the number is less than 101 we get 100 rows per country

List the employees with same salary

I have a table emp with columns :-
empid empname mgrid doj
1 Steve 2 25-03-2019
2 Winter 3 26-04-2019
3 Summer 1 27-05-2019
4 Autumn 2 28-06-2019
and a table sal with columns :-
empid project salary
1 P1 1000000
2 P1 60000
3 P2 5000
4 P3 1000000
I want to list name of employees with same salary.
Desired result :-
first_employee second_employee salary
Steve Autumn 1000000
What I tried to do is join emp table with sal and before that was trying to self join sal table. How can I achieve the desired results also is there a way to use union in order to get the results.
What if there are more than two employees with the same salary?
I would recommend group_concat():
select salary, group_concat(empname order by doj) as empnames
from emp
group by salary
having count(*) > 1;

Mysql: Get the ID where the percentage of certain element is the highest

I'm new to MYSQL, and I have following table. The goal is to return the deptID with the highest percentage of engineering employees.
empID empName job deptID salary
68 Morris secretary 3 23000
69 Maria engineer 3 32000
45 Kelly engineer 7 37000
77 Hergot engineer 7 28000
66 Hess technician 7 32000
92 Mays engineer 7 45000
89 Williams engineer 12 36000
23 Smith programmer 13 35000
56 Herr janitor 13 26000
Following is my code that doesn't work:
SELECT deptID
FROM Employee
WHERE job = 'engineer'
GROUP BY deptID
order by (SELECT COUNT(*) FROM employee WHERE job='engineer')/count(*) DESC
A desired output would be:
deptID
12
Since for deptID 12, it has only one engineer out of one employee, which has the highest percentage of engineer.
Thanks in advance for your help.
I think this is the solution. Thanks for answering.
SELECT
deptID, SUM(job = 'engineer') / COUNT(*) AS perc
FROM
Employee
GROUP BY deptID
ORDER BY perc DESC
LIMIT 1;
Here's a piece of the puzzle...
SELECT deptid
, SUM(job = '?')/?????(*) * 100 pct
FROM my_table
GROUP BY ??;
Assuming there are no ties then we're just about done, otherwise there's another step involved

mariadb SQL query showing summary lines with aggregated values

I need to write a SQL query in MariaDB to print a report with in between summary lines with aggregated values.
e.g. the data in the EMP table is:
EmpName ROLE SALARY
A Manager 10000
B operator 8000
C operator 8500
D GM 20000
E Manager 9000
I need an output like:
ROLE EmpName SALARY
Manager A 10000
E 9000
TOTAL 19000
----------------------------
GM D 20000
TOTAL 20000
----------------------------
operator B 8000
C 8500
TOTAL 16500
Many thanks in advance.
The following query can produce an output similar to the one you want:
SELECT IF(type = 1, ROLE, 'TOTAL'), SALARY
FROM (
SELECT ROLE, SALARY, 1 AS type
FROM mytable
UNION ALL
SELECT ROLE, SUM(SALARY) AS TotalSalary, 2 AS type
FROM mytable
GROUP BY ROLE) AS t
ORDER BY ROLE, type
Demo here
Try to use WITH ROLLUP modifier, for example -
SELECT
role, empname, SUM(salary)
FROM
table1
GROUP BY
role, empname WITH ROLLUP;
Output:
GM D 20000
GM (null) 20000
Manager A 10000
Manager E 9000
Manager (null) 19000
operator B 8000
operator C 8500
operator (null) 16500
(null) (null) 55500
All NULL values for grouped columns are total values. The last row is grand total value for all salaries.
GROUP BY WITH ROLLUP modifiers documentation.

SELECT MAX() FROM TABLE using GROUP BY

I have some table:
name dep_id salary
Vasia 5 1000
Oleg 5 1300
Vitia 4 1000
Sacha 3 1100
Kolia 5 1600
Lesha 2 1400
Sergey 4 1200
Marina 5 1300
Olga 5 1500
Igor 4 1400
Valia 3 1500
Erema 4 1500
I need to get the name of the employees with the maximum salary in his department
i.e I need
Lesha 1400
Valia 1500
Erema 1500
Kolia 1600
I tried:
SELECT name, max(salary) FROM employees GROUP BY dep_id
but this displays incorrect values
how can I do this?
select e1.*
from employees e1
join
(
SELECT dep_id, max(salary) as msal
FROM employees
GROUP BY dep_id
) e2 on e1.dep_id = e2.dep_id
and e1.salary = e2.msal
select t1.name,t2.sallary
from Employees t1 join
(select dep_id,MAX(Sallary) as Sallary
from Employees
group by dep_id) t2 on t1.dep_id=t2.dep_id and t1.sallary=t2.sallary
order by t2.sallary
Result:
name Sallary
---------------
Lesha 1400
Valia 1500
Erema 1500
Kolia 1600
Demo in SQL Fiddle
Try Below Query for the required output :
select name,sallary from employees t1 where (dep_id,sallary) in
(select dep_id,max(sallary) from employees group by dep_id);