insert into a table with select - sql

I have two tables with these schema:
student(id,name,dept_name, tot_cred)
instructor(id,name,dept_name,salary)
and the question is:
Insert every student whose tot_cred attribute is greater than 100 as
an instructor in the same department, with a salary of $10,000.
I try this query but I dont know how set salary for student $10,000:
insert into instructor (id,name,dept_name,salary)
select id,name,dept_name
from student
where tot_cred > 100

Just select the constant value. Personally, I'd add an alias in the select to document that the constant is intended to be the salary although it's not necessary. Different databases may have different rules for how to create that alias and you haven't told us what database you're using so I'm guessing on the syntax.
insert into instructor (id,name,dept_name,salary)
select id,name,dept_name,10000 as salary
from student
where tot_cred > 100

INSERT INTO instructor
(id,
NAME,
dept_name,
salary)
SELECT id,
NAME,
dept_name,
10000 AS salary
FROM student
WHERE tot_cred >= 100;

Related

Find average salary of employees for each department and order employees within a department by age

CREATE TABLE EMPLOYEE (
NAME VARCHAR(500) UNIQUE,
AGE INT,
DEPT VARCHAR(500),
SALARY INT
)
INSERT INTO EMPLOYEE VALUES('RAMESH',20,'FINANCE',50000);
INSERT INTO EMPLOYEE VALUES('DEEP',25,'SALES',30000);
INSERT INTO EMPLOYEE VALUES('SURESH',22,'FINANCE',50000);
INSERT INTO EMPLOYEE VALUES('RAM',28,'FINANCE',20000);
INSERT INTO EMPLOYEE VALUES('PRADEEP',22,'SALES',20000);
Could someone explain the error in the query
SELECT NAME, AGE, DEPT, AVG(SALARY)
FROM EMPLOYEE
GROUP BY DEPT
ORDER BY AGE
(/* USING NAME,AGE ETC ALSO SHOWS ERROR- "column "employee.name" must appear in the GROUP BY clause or be used in an aggregate function")
Why is there an error; consider both the logic part and the syntax part for explanation?
CAN ANYBODY PROVIDE THE ANSWER USING SUB QUERY?
You can refer to the error on at here : must appear in the GROUP BY clause or be used in an aggregate function
POSTGRESQL
SELECT DISTINCT NAME, AGE, DEPT
, AVG(SALARY)
FROM EMPLOYEES
GROUP BY NAME, DEPT, AGE
ORDER BY AGE
When you are using group by then you should extract only those columns which you used in group by and you can choose aggregate function with this. So you can use like below query.
select DEPT, AVG(SALARY) FROM EMPLOYEE GROUP BY DEPT

Is it possible to insert based on a select result in MySQL?

i have a table called employee:
And a table department
that just has id and name
I am attempting to make an insert that would add an employee to each department that exists, in a single query, is that possible?
Insert Into Employees
Select '11111', 'John', ID, 'parent'
FROM Departments
This will insert the same employee to each department, I hope I understand you correctly because it doesn't make sense to add the same employee to each department.

Getting the correct SQL subquery where nested select statement has been used

I have a table named Employee which has the following columns:
ID(primary key) of type int
Name of type varchar(255)
Designation of type varchar(50)
Salary of type int
I want to write a sub-query, which will give me the names of the employees who have greater salary than ANY employee of the designation 'Junior Officer'.
Here's what I have tried but was unsuccessful:
SELECT Name
FROM Employee
WHERE Salary>
(SELECT Salary FROM Employee WHERE Designation = 'Junior Officer');
Try this.
SELECT Name
FROM Employee
WHERE Salary>
(SELECT max(Salary) FROM Employee WHERE Designation = 'Junior Officer');
Because it is urgent, I'm writing you this without testing it. Try this:
SELECT Name
FROM Employee
WHERE Salary >
(SELECT MAX(Salary) FROM Employee WHERE Designation = 'Junior Officer');

SQL for calculating salary contribution by each department

I am writing a simple query using in oracle database that finds the salary contribution by each department.
Here are my tables:
CREATE TABLE employee (
empid NUMBER,
fname VARCHAR2(20),
deptid NUMBER,
salary NUMBER
);
CREATE TABLE department (
deptid NUMBER,
deptname VARCHAR2(20)
);
Inserting data into this table:
INSERT INTO department VALUES (1, 'Sales');
INSERT INTO department VALUES (2, 'Accounting');
INSERT INTO employee VALUES (1,' John', 1,100);
INSERT INTO employee VALUES (2,' Lisa', 2,200);
INSERT INTO employee VALUES (3,' Jerry', 1,300);
INSERT INTO employee VALUES (4,' Sara', 1,400);
Now to find out the salary contribution in percentage by each department I am using below query:
select dept.deptname, sum(emp.salary)/(select sum(emp.salary) from employee emp)*100 as percentge from employee emp, department dept where dept.deptid=emp.deptid group by dept.deptname;
Is this efficient way of calculating my output or Is there any alternate way?
Please try:
select distinct a.*,
(sum(Salary) over(partition by a.DeptID))/(sum(Salary) over())*100 "Percent"
from department a join employee b on a.deptid=b.deptid
You don't need a subquery for this. You can use analytic functions:
select dept.deptname,
100*sum(emp.salary)/(sum(sum(emp.salary)) over ()) as percentage
from employee emp join
department dept
on dept.deptid = emp.deptid
group by dept.deptname;
I also changed the join syntax to use ANSI standard joins.
EDIT:
There is not a particular "issue" with using subqueries for this. A subquery does work. In general, though, subqueries are harder to optimize than the built-in features in Oracle (and in this case in ANSI SQL). In this simple case, I don't know if there is a performance difference.
As for analytic functions, they are a very powerful component of SQL and you should learn about them.
By with clause you can calculate sum for all departments once and then use it as parameter. On your example sum value for all departments calculated for each row and this will lead to performance loss.
with t as
(select sum(salary) as sum_salary from employee)
select dept.deptname, sum(emp.salary)/ sum_salary * 100 as percentge
from employee emp, department dept, t
where dept.deptid=emp.deptid group by dept.deptname, sum_salary;

SQL: how to find maximum value items according a attribute

I am a beginner of SQL, having this table instructor:
ID name dept_name salary
001 A d01 1000
002 B d02 2000
003 C d01 3000
...
I am writing a code to find people who have highest salary in each department like:
name dept_name
C d01
B d02
I do know how to find maximum value
but I have no idea how to use it by according dept_name for all each department.
This will ensure that only records which are the highest salary for each department are returned to the result set.
SELECT name, dept_name, salary
FROM tbl t
WHERE NOT EXISTS(SELECT salary FROM tbl t2 WHERE t2.salary>t.salary AND t2.dept_name=t.dept_name)
Using SELECT name, MAX(salary) like other answerers have used won't work. Using MAX() will return the highest salary for each department, but the name will not necessarily be related to that salary value.
For example, SELECT MIN(salary), MAX(salary) is most likely going to pull values from different records. That's how aggregate functions work.
select name, max(dept_name)
from tbl
group by name
I assume it is a requirement to not include the salary in the result:
WITH INSTRUCTOR
AS
(
SELECT *
FROM (
VALUES ('001', 'A', 'd01', 1000),
('002', 'B', 'd02', 2000),
('003', 'C', 'd01', 3000)
) AS T (ID, name, dept_name, salary)
),
INSTRUCTOR_DEPT_HIGHEST_SALARY
AS
(
SELECT dept_name, MAX(salary) AS highest_salary
FROM INSTRUCTOR
GROUP
BY dept_name
)
SELECT ID, name, dept_name
FROM INSTRUCTOR AS T
WHERE EXISTS (
SELECT *
FROM INSTRUCTOR_DEPT_HIGHEST_SALARY AS H
WHERE H.dept_name = T.dept_name
AND H.salary = T.highest_salary
);
You can use the group by clause. Check this w3Schools link
SELECT NAME,DEPT_NAME,max(SALARY) FROM table_name group by DEPT_NAME