employee
id name
1 mark
2 Clark
3 Johnson
4 peter
emp_salary
empid sal
1 2000
2 4000
2 null
4 6000
5 null
I need employee name whose salary is null...and also i need show 5000 salary where salary is null, without inserting value in emp_salary table
I tried
Select e.name,e.empid,e.salary
from table employee e inner join emp_salary em on e.id=em.empid
where em.salary=null
but this query is not working
I need to select null value and show 5000 on null value without affecting table
Select e.name,e.empid,'5000' salary
from table employee e inner join emp_salary em on e.id=em.empid
where em.salary is null
check for null with "IS NULL" not "=NULL"
This will show all employees whose salary is null, but your select will show the "salary" as 5000
Related
**i have a 3 tables in mysql **
1- Client Table
PK
NUM_CLIENT
NAME_CLIENT
1
ALEX
2
ADAM
2- Invoice Table
FK
NUM_CLIENT_INV
INV_VALUE
1
1000
1
2000
3- Sales returns Table
FK
NUM_CLIENT_SR
Return_VALUE
1
300
1
400
my query is
SELECT NAME_CLIENT , NUM_CLIENT , INV_VALUE , Return_VALUE FROM client
LEFT JOIN Invoice
ON NUM_CLIENT_INV = NUM_CLIENT
LEFT JOIN Sales returns
ON NUM_CLIENT_SR = NUM_CLIENT
The expected result is :
NAME_CLIENT
NUM_CLIENT
INV_VALUE
Return_VALUE
ALEX
1
1000
null
ALEX
1
null
300
ALEX
1
2000
null
ALEX
1
null
400
ADAM
2
null
null
but the result was given is :
NAME_CLIENT
NUM_CLIENT
INV_VALUE
Return_VALUE
ALEX
1
1000
300
ALEX
1
1000
400
ALEX
1
2000
300
ALEX
1
2000
400
ADAM
2
null
null
there is duplicate in result in columns INV_VALUE and Return_VALUE
What Is The Right Query That Give Me The expected result ?
Your existing joins return all combinations of client, optional invoice, and optional sales rows for each client.
If you want invoices and sales on separate rows, you need to join them in separate queries and union those queries. Use an inner joins so that if there are neither sales nor invoices you don't get two null/null rows, but then you have to add an extra query to get the null/null row when there are neither.
SELECT NAME_CLIENT , NUM_CLIENT , INV_VALUE , NULL as Return_VALUE FROM client
INNER JOIN Invoice
ON NUM_CLIENT_INV = NUM_CLIENT
UNION ALL
SELECT NAME_CLIENT , NUM_CLIENT , NULL , Return_VALUE FROM client
INNER JOIN Sales returns
ON NUM_CLIENT_SR = NUM_CLIENT
UNION ALL
SELECT NAME_CLIENT , NUM_CLIENT , NULL , NULL FROM client
LEFT JOIN Invoice
ON NUM_CLIENT_INV = NUM_CLIENT
LEFT JOIN Sales returns
ON NUM_CLIENT_SR = NUM_CLIENT
WHERE NUM_CLIENT_INV IS NULL AND NUM_CLIENT_SR IS NULL
ORDER BY NUM_CLIENT
I want to get the output of 2 tables by removing the NULL
Emp Table
id name dept
1 Null EE
2 Ravi Null
NULL Mani CSE
Stud Table
id name dept
1 Manju NULL
2 NULL ECE
3 Mani CSE
Output
id name dept
1 Manju EE
2 Ravi ECE
3 Mani CSE
You can try below - using left join and coalesce() function
select a.id, coalesce(a.name,b.name) as name, coalesce(a.dept,b.dept)
from stud a left join emp b
on a.id=b.id
I don't know why your ID is nullable, from what I can see it is the primary key of you table. But if you want to select all data removing the NULL. Just add a condition.
For example:
SELECT COALESCE(e.name, s.name) name,
COALESCE(e.dept, s.dept) dept
FROM emp e
LEFT JOIN stud s
ON s.id = e.id
But honestly, you should check the way you created your database. As someone commented, this is not a relational table.
Would it be possible to combine a having and a where clause in an OR operator within a single SQL query?
Maybe not the best example, but you'll get the idea:
Select the departments from the employee table that is HR (using the where clause) OR that pays all employees more than 25000 (using the having clause).
So how do we get the OR condition in the query down below? Or would it be better to separate the query into 2 queries.
SELECT dept, SUM (salary)
FROM employee
WHERE dept = "HR"
GROUP BY dept
HAVING SUM (salary) > 25000
The below will work - you do not have to specify an aggregate in the HAVING clause
SELECT dept, SUM (salary)
FROM employee
GROUP BY dept
HAVING dept = "HR" or SUM (salary) > 25000
But your statement "that pays all employees more than 25000 " is not clear. Do you want
Departments where all employees earn over 25000 each, or
departments where all employees earn over 25000 in total?
The query above gives you the second option, as that is closest to your original query
Wrap the GROUP BY part up in a derived table. Then apply the conditions to its result:
select dept, salarysum
from
(
SELECT dept, SUM (salary) as salarysum
FROM employee
GROUP BY dept
) dt
where salarysum > 25000 or dept = "HR"
Or, perhaps, "that pays all employees more than 25000", means that no dept employee earns less than 25000?
select dept, minsalary
from
(
SELECT dept, MIN(salary) as minsalary
FROM employee
GROUP BY dept
) dt
where minsalary > 25000 or dept = "HR"
Maybe you want dept where all salaries are over 25000.
drop table if exists employees;
create table employees(id int auto_increment primary key, dept varchar(2), salary int);
insert into employees (dept,salary)
values
('HR',10000),('aa',10000),('aa',45000),('bb',25000),('cc',26000),('cc',26000);
select dept,sum(salary) sumsalary,count(*) obs, sum(case when salary > 25000 then 1 else 0 end) over25000
from employees
group by dept having obs = over25000 or dept = 'hr'
+------+-----------+-----+-----------+
| dept | sumsalary | obs | over25000 |
+------+-----------+-----+-----------+
| cc | 52000 | 2 | 2 |
| HR | 10000 | 1 | 0 |
+------+-----------+-----+-----------+
2 rows in set (0.01 sec)
Here is a simplified version of the table structure.
Employee
(
ID GUID NOT NULL
OldID GUID NULL
Name VARCHAR(50) NOT NULL
CreationDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
It contains employee information as well as any changes been made to employee attributes. This way we can get a complete audit of changes been made. When OldID is NULL, that basically mean the latest data. Here is an example, I am using integer values for identifier to make this example easier to understand.
ID OldId Name CreationDate
13 NULL John 15-July-2013
12 13 John1 14-July-2013
11 12 John2 13-July-2013
10 11 John3 12-July-2013
121 NULL Smith 15-July-2013
To start with I can get the unique employees from table by
SELECT ID, Name FROM Employee WHERE OldId IS NULL
I am looking to get latest ID but its earliest name. So that result should be two rows
ID Name
13 John3
121 Smith
I am not sure how can I get these results. Any help will be highly appreciated.
Here's one approach that works for your data:
with groups as
(
select groupID = ID, *
from Employee
where OldID is null
union all
select g.groupID, e.*
from groups g
inner join Employee e on g.ID = e.OldID
)
, ranks as
(
select *
, firstRank = row_number () over (partition by groupID order by creationDate)
from groups
)
select ID = groupID
, Name
from ranks
where firstRank = 1
SQL Fiddle with demo.
I need to delete duplicate record from table in mysql.
So i have a table name "employee" fields are empid, empname, empssn
for getting duplicate record i have written a query
SELECT COUNT(empssn), empssn FROM employee
GROUP BY empssn
HAVING COUNT(empssn) > 1
Now I want to delete duplicate records. For that I have written query is.
DELETE FROM employee
WHERE (empid, empssn) NOT IN (
SELECT MIN(empid), empssn FROM employee
GROUP BY empssn
);
you can assume records in table are
EmpId EmpName EmpSSN
-------------------------------
1 Jack 555-55-5555
2 Joe 555-56-5555
3 Fred 555-57-5555
4 Mike 555-58-5555
5 Cathy 555-59-5555
6 Lisa 555-70-5555
7 Jack 555-55-5555
8 Mike 555-58-5555
9 Cathy 555-59-5555
10 Lisa 555-70-5555
11 Lisa 555-70-5555
but I have a mysql error is
You can't specify target table 'employee' for update in FROM clause
Does the trick of wrapping it into a derived table work for this case? (Based on http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/)
DELETE FROM employee
WHERE (empid, empssn) NOT IN (
SELECT empid, empssn FROM (
SELECT MIN(empid) AS empid, empssn FROM employee
GROUP BY empssn
) X
);
Edit Yep it seems to work this end.