How to get highest salary city wise - mysql

I was asked this question in an interview (hopefully you guys can help me; thanks in advance).
In Hive, how would you get the highest salary city wise from employee table?
003 Amit Delhi India 12000
004 Anil Delhi India 15000
005 Deepak Delhi India 34000
006 Fahed Agra India 45000
007 Ravi Patna India 98777
008 Avinash Punjab India 120000
009 Saajan Punjab India 54000
001 Harit Delhi India 20000
002 Hardy Agra India 20000

Try this:
SET #rank:=0;
SET #dept:='';
SET #desiredrank=8; --For example.
SELECT ename, rank, salary
FROM
(
SELECT
ename, salary,
#rank:=CASE WHEN #dept=deptid THEN #rank+1 ELSE 1 END AS rank,
#dept:=deptid AS department
FROM employees e
JOIN departments d
ON e.deptid=d.deptid
ORDER BY d.deptid, salary
)
WHERE rank=#desiredrank
Basically you have to use two extra variables. One to simulate the grouping, and one to keep track of the rank. When that query is done, filter it for the rank you want.

Select Id , max(salary) from employee group by city;
In that case, every city with highest salary with respective id will be displayed.

Related

SQL Teradata - mark duplicate records in the column

So I know how to identify a dup row but now also need to identify the row linked to it and mark as duplicate.
Ex:
Row Name ID State Date Dup
---------------------------------------
001 Jim 001 NJ jan2020
002 Jim 001 NJ jan2020
003 Tan 002 NY feb2020
004 Allen 003 CA Feb2020
Output should like:
Row Name ID State Date Dup
---------------------------------------
001 Jim 001 NJ jan2020 Y
002 Jim 001 NJ jan2020 Y
003 Tan 002 NY feb2020 N
004 Allen 003 CA Feb2020 N
I can use partition using row_number but it will not flag the record 001 as Y. What could be an approach?
If you have a small number of columns, you can do something like this:
SELECT Row, Name, ID, State, Date,
CASE
WHEN COUNT(*) OVER(PARTITION BY Name, ID, State, Date) > 1 THEN 'Y'
ELSE 'N'
END AS Dup
FROM MyTable
This marks a given row as a duplicate based on the columns specified in the PARTITION BY expression. Also, be careful with your column names (i.e. Row, Date), as they may be reserved words.

How to count number of occurrence in a filed table

if I have a table with a field City like this:
City
------
London
Los Angeles
London
Athens
Rome
Paris
Paris
How could I get this result?
City | CityNumb
-------------------
London 2
Athens 1
Los Angeles 1
Paris 2
Rome 1
select City, count(*) as CityNumb
from your_table
group by City
Above image shows the SQL query and results for above task

How to display data without using JOIN and Counting Number of employees?

I am having trouble with this question I can't seem to get the count correct
on each department and only select the highest one as well as excluding
"DALLAS"
THIS IS THE QUESTION
"Write a SQL statement to display the name and location of all departments
(except the departments located in Dallas) with the highest number of
employees.
You cannot use join operations in your SQL statement (e.g., … FROM department,
employee WHERE …, department INNER JOIN employee ON …)."
DEPARTMENT_ID DEPARTMENT_NAME LOCATION
------------- -------------------- --------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 IT DALLAS
50 EXECUTIVE NEW YORK
60 MARKETING CHICAGO
6 rows selected
EMPLOYEE_ID EMPLOYEE_NAME JOB_TITLE SUPERVISOR_ID HIRE_DATE SALARY COMMISSION DEPARTMENT_ID
----------- -------------------- -------------------------------------------------- ------------- --------- ---------- ---------- -------------
7839 KING PRESIDENT 20-NOV-01 5000 50
7596 JOST VICE PRESIDENT 7839 04-MAY-01 4500 50
7603 CLARK VICE PRESIDENT 7839 12-JUN-01 4000 50
7566 JONES CHIEF ACCOUNTANT 7596 05-APR-01 3000 10
7886 STEEL PUBLIC ACCOUNTANT 7566 08-MAR-03 2500 10
7610 WILSON BUSINESS ANALYST 7596 03-DEC-01 3000 20
7999 WOLFE TEST ANALYST 7610 15-FEB-02 2500 20
7944 LEE REPORTING ANALYST 7610 04-SEP-06 2400 20
7900 FISHER SALES EXECUTIVE 7603 06-DEC-01 3000 500 30
7921 JACKSON SALES REPRESENTATIVE 7900 25-FEB-05 2500 400 30
7952 LANCASTER SALES CONSULTANT 7900 06-DEC-06 2000 150 30
7910 SMITH DATABASE ADMINISTRATOR 7596 20-DEC-01 2900 40
7788 SCOTT PROGRAMMER 7910 15-JAN-03 2500 40
7876 ADAMS PROGRAMMER 7910 15-JAN-03 2000 40
7934 MILLER PROGRAMMER 7876 25-JAN-02 1000 40
8000 BREWSTER TBA 22-AUG-13 2500
8100 PHILLIPS TBA 7839 21-AUG-13 2800
7400 SMITH VICE PRESIDENT 7839 16-FEB-01 4300 50
7700 ANDRUS PUBLIC ACCOUNTANT 7566 18-FEB-02 2500 10
7601 SAMPSON PROGRAMMER 7910 09-JAN-01 2500 40
7588 DODSON TEST ANALYST 7610 02-AUG-08 2500 20
7888 SANDY SALES CONSULTANT 7900 05-AUG-04 2500 30
22 rows selected
SELECT DEPARTMENT_NAME,
location,
count(*)
FROM DEPARTMENT
WHERE department_id IN ( SELECT department_id
FROM department
WHERE UPPER(location) <> 'DALLAS'
)
group by department_NAME, location
ORDER BY location;
DEPARTMENT_NAME LOCATION COUNT(*)
-------------------- -------------------- ----------
MARKETING CHICAGO 1
SALES CHICAGO 1
ACCOUNTING NEW YORK 1
EXECUTIVE NEW YORK 1
you can try using sub-queries if you are limited in not using joins
SELECT *
FROM (SELECT d.department_name,
d.location,
(SELECT COUNT(employee_id)
FROM employee e
WHERE e.department_id = d.department_id) no_employees
FROM department d
WHERE d.location <> 'DALLAS'
) t
WHERE no_employees = (SELECT COUNT(employee_id)
FROM employee
WHERE department_id IN (SELECT DISTINCT department_id
FROM department
WHERE location <> 'DALLAS')
GROUP BY department_id
ORDER BY 1 DESC
LIMIT 1)
Result
department_name location no_employees
SALES CHICAGO 4
EXECUTIVE NEW YORK 4
Am trying to the find the department with maximum count and then retrieve the corresponding name and location without using joins
SELECT (SELECT DEPARTMENT_NAME, location
FROM DEPARTMENT
WHERE department_id = q.department_id) ,
q.ct countofdept
FROM
(SELECT count(*) ct, department_id
FROM EMPLOYEE
WHERE department_id in ( SELECT department_id
FROM department
WHERE UPPER(location) <> 'DALLAS'
)
GROUP BY department_id
ORDER BY ct desc
LIMIT 1) q

Mysql unable to get aggregate query

I have a table with values as follows(MySql):
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
but I need them in the following less convenient format:
DEPTNO ENAME
------ -----------------------------------------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
Kindly help me to achiever above result/output.
Using group_concat
SELECT deptno, group_concat(ename order by ename) as ename
FROM emp
group by deptno
ORDER BY deptno;

Mysql Query Studied Days

I have Emp table with following values
Emp_Id Emp_Name Subject Dates
001 Smith Java 07-02-2012
001 Smith oracle 08-02-2012
001 smith C++ 10-02-2012
002 john java 01-01-2012
002 john SE 10-01-2012
002 john c 10-01-2012
001 smith physics 04-01-2012
001 smith c# 07-02-2012
001 smith javascript 07-02-2012
Now as we can see here smith studied only 3 days for month February and 1 for month Jan
while john studied only 2 days for month January.
How can we calculate this count for any employee?
As a e.g:Output should be in following way.
Emp_Id Emp_Name Month_Year No_Of_Days_Studied_In_Month
001 smith Feb12 3
001 smith Jan12 1
002 john Jan12 2
You can GROUP BY YEAR(Dates), MONTH(Dates) and do a COUNT.
TRY:
SELECT emp_id,
emp_name,
Date_format(DATE, '%b%y') AS dates,
COUNT(*) AS No_Of_Days_Studied_In_Month
FROM emp
GROUP BY Date_format(DATE, '%b%y'), emp_name
ORDER BY emp.emp_id