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

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.

Related

insert into a table with select

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;

SQL IF two IDs are equal then select

I am trying to get correct result but I can not find right way to get.
I have this table:
I would like to get result like this one
This is my code:
SELECT employee_ID, first_name, manager_id ,
(SELECT first_name from employees where MANAGER_ID= employee_id)
from employees
I got only first 3 columns and fourth one is empty because its wrong select
Do self join to the same table
select emp1.employee_ID, emp1.first_name, emp1.manager_id,emp2.first_name as manager_name from employees emp1
left join employees emp2
on emp1.manager_id=emp2.employee_id;
Sorry my bad explanation, I have 5 columns, first one in not in the picture because its only ID of row, my first row is Employee id, second one is first_name, third one is manager id and fourth one should be first name of manager. Manager name is that when manager id is equal to employee id, so e.g. employee id = 206, first name is William, manager id is 205. and name should be Shelley because Shelley's employee ID is 205

Insert record into table using default values and values selected from another table using where clause

I know it is possible to insert records into a table by using a select statement on a different table, but I need to use a where clause to select which record. For example,
INSERT INTO Employee_Archive(EmployeeID, Name, ArchiveReason)
SELECT EmployeeID FROM Employees, Name from Employees, 'Retired'
WHERE EmployeeID = '001'
I hope that example makes sense. I wish to get the EmployeeID and the Name from the Employees table, and add my own ArchiveReason value, but I need to specify by which EmployeeID. Cheers
You can simply add a WHERE clause in your SELECT statement:
SELECT
EmployeeID,
Name,
'Retired'
FROM Employees
WHERE EmployeeID = '001'

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;

How to insert a foreign key using a Sub-SELECT in SQL Server

I've just started working with SQL Server for the first time and I'm having trouble populating test data. I have two tables where one has a foreign key to the other and I would like to be able to insert a new record using the following SQL:
insert into Employee (
EmployeeName,
DepartmentId
) values (
"John Doe",
(select Id from Department where DepartmentName = 'Accounting')
);
This statement works fine in Oracle but in SQL Server I get an error saying:
Subqueries are not allowed in this context.
Does anybody know the right way to do this in SQL Server?
INSERT INTO Employee
(EmployeeName, DepartmentId)
SELECT
'John Doe' AS EmployeeName, Id AS DepartmentId
FROM
Department WHERE DepartmentName = 'Accounting';
You can do:
insert into Employee (
EmployeeName,
DepartmentId
)
SELECT 'John Doe', Id
FROM Department
WHERE DepartmentName = 'Accounting'
Your query will fail in Oracle if there is more than one accounting department.
If you rely on this behavior, use this syntax:
INSERT
INTO Employee
(
EmployeeName,
DepartmentId
)
SELECT "John Doe",
(
SELECT id
FROM Department
WHERE DepartmentName = 'Accounting'
)
Otherwise, just use the SELECT FROM Department syntax proposed by others.
Note, however, that this syntax will insert John Doe twice or more, if there are multiple rows with name set to Accounting in Deparments.