how to write this query from 2 tables - sql

Hear no relation have 2 table
"employee Table"
columns
Empid Empname Salary Age
1 abc 2000 20
2 xyz 3000 26
3 ijk 4000 32
4 mno 5000 50
"Groupname" table
columns
Groupid Groupname Min Max
1 young 18 25
2 middle 26 35
3 old 36 60
then i need result using both tables
empid empname age group
1 abc 2000 20 young
2 xyz 3000 26 middle
3 ijk 4000 32 middle
4 mno 5000 50 old

select empid,empname, salary, grp.groupname from employee emp left
outer join groupname grp
on ( emp.age > grp.[min] and emp.age <
grp.[max])

Try this:
select Empid, Empname, Salary, Age,Groupname
from Employee,GroupNameTable
where Age between Min and max

Related

List the emps whose sal > his Manager but less than other Managers

List the emps whose sal > his Manager but salary less than other Managers
Table
emp_ID emp_Name emp_sal_K emp_manager
1 Ali 200 3
2 Zaid 620 4
3 Mohd 1140 2
4 LILY 600 NULL
5 John 1240 6
6 Mike 1160 4
7 John 1240 6
8 Mohd 1640 2
Query
select *
from emp_demo2 e1
where emp_sal_K >
(select emp_ID from emp_demo2 e2
where e1.emp_manager = e2.emp_ID and e2.emp_sal_K
< all
(select emp_id from emp_demo2 e3
where e2.emp_ID = e3.emp_id))
Result: null columns. There are 4 managers in total 2,4 ,6 and 3. Here clearly emp_id 2 Zaid is a person whose salary 620 is greater than his manager's salary emp_id 4 which is 600 but less than all other managers emp_id 6 and 3. So I should get that result but getting nothing.
Expected result
emp_ID emp_Name emp_sal_K emp_manager
2 Zaid 620 4
SELECT emp.*
FROM emp_demo2 emp
LEFT JOIN emp_demo2 mgr
ON emp.emp_manager = mgr.emp_id
WHERE emp.emp_sal_k > mgr.emp_sal_k
AND emp.emp_sal_k < (SELECT Min(mgr1.emp_sal_k)
FROM emp_demo2 emp1
JOIN emp_demo2 mgr1
ON emp1.emp_manager = mgr1.emp_id
WHERE mgr1.emp_id <> emp.emp_manager
AND mgr1.emp_id <> emp.emp_id)

SQL query to join two tables without common column in oracle DB

I have 2 tables employee and job_role as below. I need to write a SQL query to find designation of each employee by joining this table.
Input Table
Employee
e_id e_name Salary
-----------------------
1 ABC 1000
2 CDE 2000
3 GHI 3500
4 JKL 5000
5 MNO 4000
6 XYZ 3000
Job_role
Designation Sal_min Sal_max
-------------------------------
Associate 1000 2000
Lead 2001 3000
Manager 3001 5000
Problem: if salary from employee table is in the range sal_min to sal_max, find the designation
Desired output:
e_id e_name Salary Designation
-----------------------------------
1 ABC 1000 Associate
2 CDE 2000 Associate
3 GHI 3500 Manager
4 JKL 5000 Manager
5 MNO 4000 Manager
6 XYZ 3000 Lead
The ON clause can consist of other operations than =. Here you can use <= and >= (or BETWEEN if you like).
SELECT E.E_ID,
E.E_NAME,
JR.DESIGNATION
FROM EMPLOYEE E
LEFT JOIN JOB_ROLE JR
ON JR.SAL_MIN <= E.SALARY
AND JR.SAL_MAX >= E.SALARY;
(Note: I used LEFT JOIN for the case that there are any employees where the salary doesn't match. I guessed you rather want to see them with a NULL designation than not at all.)

Need to pick up the SUM OF Tax Amount for the highest Sequence number Per Year, Per SSN, Per employer

Consider Employee table:
Employerid ssn year Seqnumber q1taxamt q2taxamt q3taxamt q4taxamt
1004 101 2013 1 2000 0 0 0
1004 101 2013 2 2000 100 0 0
1004 101 2013 3 2000 100 200 0
1004 101 2013 4 2000 100 200 300
1004 102 2013 1 3000 0 0 0
1004 102 2013 2 3000 200 0 0
1004 102 2013 3 3000 200 300 0
1004 102 2013 4 3000 200 300 400
1004 102 2013 5 3000 200 300 400
Here the transformation rule is we need to pick the highest Seqnumber with respect to each ssn per year per
Employerid and the amounts.
i.e for 10004 for sum(q1taxamt) is 2000 +3000 = 5000
The Logic is ssn 101 has highest seq number of 4 and ssn 102 has highest seq number of 5 so we need to pick those values wrt to employerid
Example:
Want to check for q1taxamt: 2000 +3000 = 5000
Want to check for q4taxamt: 300 +400 = 700
output must be:
Employerid YEAR q1taxamt q2taxamt q3taxamt q4taxamt
10004 2013 5000 300 500 700
The below query is generating wrong result:
Select
Sum(E1.q1taxamt) q1taxamt,
Sum(E1.q2taxamt) q2taxamt,
Sum(E1.q3taxamt) q3taxamt,
Sum(E1.q4taxamt) q4taxamt,
E1.Employerid,
E1.YEAR
from Employee E1
join
(
select
E.Employerid,
MAX(E.seqnumber) seqnumber,
E.YEAR
from Employee E
group by E.Employerid,E.SSn,E.year
)E2
on E1.Employerid=E2.Employerid
AND E1.YEAR=E2.YEAR
and E1.seqnumber=E2.Taxseqnumber
Just use row_number():
select e.*
from (select e.*,
row_number() over (partition by E.Employerid, E.SSn, E.year
order by e.seqnumber desc
) as seqnum
from Employee e
) e
where seqnum = 1;
For best performance, you want an index on Employee(EmployerId, SSN, seqnumber desc).
You missing SSN join predicate between E1 and E2 thats why you are getting wrong result. I think this might be faster than Row_Number method.
Select
Sum(E1.q1taxamt) q1taxamt,
Sum(E1.q2taxamt) q2taxamt,
Sum(E1.q3taxamt) q3taxamt,
Sum(E1.q4taxamt) q4taxamt,
E1.Employerid,
E1.YEAR
from Employee E1
join
(
select
E.Employerid,
E.SSn,
MAX(E.seqnumber) seqnumber,
E.YEAR
from Employee E
group by E.Employerid,E.SSn,E.year
)E2
on E1.Employerid=E2.Employerid
AND E1.YEAR=E2.YEAR
AND E1.SSN = E2.SSN --Here
and E1.seqnumber=E2.Taxseqnumber

Select statement with multiple rows from condition on values in single column affecting more than one column

I have the table below.Using salary as condition I want to get multiple rows. Below is current table call it employee.
empid | name | salary
-----------------------------------
1 A1 alex 20000
2 B2 ben 4500
3 C1 carl 14000
compare the salary to certain fixed values, and every time the salary is larger than the fixed value, show a record in output.Also output a calculated tax column.Below is a step closer to my final result
select e.*,tax= (case when salary<6000 then tax=0.06 *salary,when salary between 6000 and 18000 then tax= 0.06 *(salary -6000),else tax =0 ),m.incometype,
from employee e
left join
(
select 0 as threshold, 101 as incometype
union
select 5999 as threshold, 102 as incometype
union
select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid
Desired ouput would be:
empid | name | salary | incometype | tax
----------------------------------------------
1 A1 alex 20000 101 360
2 A1 alex 20000 102 720
3 A! alex 20000 103 0
4 B2 ben 4500 101 270
5 C1 carl 14000 101 360
6 C1 carl 14000 102 480
this is a question further to Select statement with multiple rows from condition on values in single column
this may help you
select e.*,
case
when salary<6000 then (0.06*salary)
when salary between 6000 and 1800 then (0.06*(salary -6000))
when m.incometype=103 then 0
end as tax
,m.incometype,
from employee e
left join
(
select 0 as threshold, 101 as incometype
union
select 5999 as threshold, 102 as incometype
union
select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid

Oracle SQL Rollup Query : Determine the start of group

How can we determine the start of a Group in Rollup ?
And the include the group value by concatinating it with some useful text and display it as first row of the group ?
e.g:
dept emp_name sal
---- -------- ---
10 sac 999
10 abc 888
20 pqr 777
20 lmn 123
30 stv 444
30 com 555
o/p after rollup should be
dept emp_name sal
---- -------- ---
Department 10 NULL NULL
10 sac 999
10 abc 888
20 1887
Department 20 NULL NULL
20 pqr 777
20 lmn 123
20 900
Department 30 NULL NULL
30 stv 444
30 com 555
30 999
3786
Here is the query with which you can achieve the requirement. It uses GROUP BY ROLLUP
SELECT DISTINCT 1 AS rn,dept, CONCAT('Department ', dept) AS DeptDesc, NULL AS Sum
FROM your_table
UNION ALL
SELECT 2, dept, CAST(dept AS VARCHAR(10)), SUM(SAL)
FROM your_table
GROUP BY ROLLUP(dept, emp_name)
ORDER BY dept, rn, sum
Here is the code at SQL Fiddle