Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Employee (emp_no, emp_fname, emp_lname, emp_salary,emp_comm, job_title)
use RELATIONAL SET OPERATOR
-- Write a sql query that calculates the total salary for all employees
-- You need to add the salary and commission
-- Note that some employees don't get a commission (here commission is null)
-- You must need UNION
Please help me to find the answer.
Not sure why you need UNION to do this, Try this one
select sum(ifnull(emp_salary,0)+ifnull(emp_comm,0)) As Total
from yourtable
If you need UNION answer try this
SELECT SUM(salary)
from
(
select sum(emp_salary) As salary
from yourtable
WHERE emp_salary IS NOT NULL
UNION ALL
select sum(emp_comm)
from yourtable
WHERE emp_comm IS NOT NULL
)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
So I have two tables and i need to write the SQL queries to display the total amount earned by each employee. tables in picture below
i tried to multiple combinations but it keeps giving me an error. could anyone please show me the way ?
here is the code i tried,
SELECT Employee.FirstName, Employee.LastName, Payments.Value
FROM Payments
LEFT JOIN Employee
ON Employee.EmployeeID = Payements.EmployeeID
ORDER BY Employee.EmployeeID
SELECT Sum(Value) FROM Payments Group By EmployeeID;
You should group and sum the amounts and then than result joined by employeeId with employees to get the specific data, as name, etc.
SELECT
EmployeeId,
FirstName,
LastName,
sm.Total as Total,
FROM
Employees em
JOIN
(SELECT
EmployeeId,
SUM(Value) as Total
FROM
Payments
GROUP BY EmployeeId) sm
ON sm.EmployeeId = em.EmployeeId
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a student table where column is name, attendance, and date in which data is entered everyday for the students who attend the class. For example if a student is absent on a day, entry is not made for that particular student for that day.
Finally. I need to find out students name whose attendance less than 50.
You can use GROUP BY and HAVING statements for this.
SELECT name FROM student GROUP BY name HAVING COUNT(*) < 50;
Please note that above query is not tested.
You have to use GROUP BY clause to aggregate similar student in table and HAVING check your condition, to get your desired output.
SELECT name, count(name)
FROM student
GROUP BY column_name
HAVING count(name)<50;
I hope this will help to solved your problem.
SELECT name
FROM StudentsTable
WHERE COUNT(name) < 50
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table employee like :
name salary
jhon 5000
jaz 5000
raja 1234
rubi 1234
I need to get output like
name salary
jhon 5000
raja 1234
NAME should be anyone (jhon and jaz) OR (raja and rubi) for salary 5000,1234 respectively
There's no way to easily express "any" in SQL - you tend to have to give a rule, even if you don't care.
So,
select MIN(Name) as Name,Salary from employee group by salary
Will arbitrarily select the name that sorts earliest alphabetically.
this is one of the other way of getting result
with cte as
(
select *,ROW_NUMBER() over (partition by salary order by name)as rn from table)
select * from cte where rn=2
fiddle demo
You Question is quite incomplete including your criteria but see if this suffice your needs
select name, Salary from [Employee] where Salary = '5000'
will return
jhon 5000 and jaz 5000
select name, Salary from [Employee] where Salary = '1234'
will return
raja 1234 and rubi 1234
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am getting semantic error when i am trying to run this query, not able to see any error.
Find the ssn of employee with lowest salary
select ssn from employee
where salary < ALL
( select salary from employee);
You trying to find a salary that's lower than all other salaries. That won't work because the minimum salary itself is in the table. When we reach the minimum salary, it gets compared to all other salaries in the table. But the comparison < will fail when we compare the minimum salary to itself.
You need to change < to <=.
SELECT ssn FROM employee
WHERE salary <= ALL (SELECT salary FROM employee);
This will return the ssn's for all employees that have the minimum salary. Equivalently,
SELECT ssn FROM employee
WHERE salary = (SELECT MIN(salary) FROM employee);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a table consisting of the following columns:
billid, patientid, doctorid, fees
How do i display the doctors who treat more than one patient?
TRIED THE FOLLOWING CODE and got it.
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
Thanks :)
SELECT doctorID
FROM YourTable
GROUP BY doctorID
HAVING COUNT (DISTINCT patientid) > 1
These are basic SQL queries. If you have trouble with something like this, you should really get to some SQL tutorial or book first.
select doctorid, count(patientid) from table1 group by doctorid having COUNT (DISTINCT patientid) > 1 ;
This will show you the doctor list having more than 1 distinct patient
on the given information if you will just select doctorid who is treating more than one patient with this query
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
and then you can use that doctorid in rest of your operations