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

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'

Related

Using Array statements in SQL together with Where clause outputs extra rows with null values but it shouldnt

I started experimenting with BigQuery and I encountered SQL syntaxes such as array and unnest. I created a nested table (is this the correct name? ) and wrote an example query(below)
I wanted to get the department of 'enes' so I wrote this query:
SELECT
dept,
ARRAY(SELECT employeeName FROM UNNEST(name) AS employeeName
WHERE ( (employeeName LIKE 'enes') and
(employeeName is not null))) AS EmpNameDisplay
FROM EmployeeTable
however results looks like this :
Why it brings the second row? What if I had million departments and it will try to bring them all?
I reformed the table in a way it doesnt include nesting tables. And when I use where statement to choose the department of 'enes' it only showed only one result which is correct. Why this nesting causes such abnormaly?
I suppose you need something like this:
with EmployeeTable as
(
select 'Data Engineer' as dept, ['enes','emre','john'] as name
union all select 'Manager' as dept, ['machiavelli','john'] as name
)
select t1.dept,
employeeName
from EmployeeTable t1,
UNNEST(t1.name) as employeeName
where employeeName = 'enes'
;
If you don't need employee name in the result list (department name only), you can use this:
with EmployeeTable as
(
select 'Data Engineer' as dept, ['enes','emre','john'] as name
union all select 'Manager' as dept, ['machiavelli','john'] as name
)
select t1.dept
from EmployeeTable t1
where 'enes' in UNNEST(t1.name)
;

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.

Oracle SQL - Issue with inserting values into columns and foreign keys

I have a table which contains 2 foreign keys from 2 seperate tables as well as normal columns. I have managed to insert the data into the foreign keys however I am having trouble inserting data into the normal columns. Here is my statement
INSERT INTO Emp_LC
(EmpID, fName, sName, lCCode, Date)
SELECT * FROM
((SELECT EmpID FROM Employees WHERE jobDesc = 'Driver' OR Employees.jobDesc = 'DRIVER')),
((SELECT lCCode FROM LicCerts WHERE Desc = 'Driving Licence'));
I want to add a value for fName, sName and Date to this statement but I cant figure out where to place them. I think I need 'VALUES' somewhere within the statement.
Thanks in advance.
Assuming these come from the employees table, add them to the subquery and the outer query:
INSERT INTO Emp_LC(EmpID, fName, sName, lCCode, Date)
SELECT EmpId, fName, sName, lCCode, date
FROM (SELECT EmpID, fName, sName
FROM Employees
WHERE jobDesc = 'Driver' OR Employees.JOBDESCRIPTION = 'DRIVER'
) CROSS JOIN
(SELECT lCCode, date
FROM LicCerts
WHERE Desc = 'Driving Licence'
);
This assumes date comes from LicCerts.

Retrieving data with single occurrence of repeated data

SELECT *
FROM employee
GROUP BY first_name
HAVING count(first_name) >= 1;
How can i retrieve all rows and columns with single occurrence of duplicates? i want to retrieve all the table contents including repeated data that must occur only at once. In a table first_name,last_name are repeated twice but with different in other info.
Please Help.
try this Sql Query
SELECT * FROM EMPLOYEE WHERE FIRST_NAME NOT IN
(
SELECT FIRST_NAME FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY FIRST_NAME ORDER BY FIRST_NAME) RNK,FIRST_NAME FROM EMPLOYEE
)A WHERE A.RNK=2
)

How to get all the columns, but have to group by only 2 columns in sql query

I have a table Employees, which has Fields as below:
Employee_name,Employee_id,Employee_status,Employee_loc,last_update_time.
This table does not have any constraint.
I have tried the below query.
select Employee_name, count(1)
from Employees
where Employee_status = 'ACTIVE'
Group by Employee_name,Employee_loc
having count(Employee_name) > 1
order by count(Employee_name) desc
In the select, I need to get Employee_id too.. Can any one help on how to get that?
You can just add Employee_id to the query, and also add it to the group by clause. (Adding it to the grouping won't make any difference in the query results, assuming each employee name each employee id is unique).
If the grouping does make a difference, that implies that some combinations of employee name and location have more than one ID associated with them. Your query would therefore need to decide which ID to return, possibly by using an aggregate function.
SELECT EMPLOYEE_NAME, EMPLOYEE_ID, COUNT(1)
FROM
EMPLOYEES
WHERE
EMPLOYEE_NAME IN
(
SELECT EMPLOYEE_NAME
FROM EMPLOYEES
WHERE Employee_status = 'ACTIVE'
GROUP BY Employee_name,Employee_loc
HAVING COUNT(*) > 1
)
GROUP BY EMPLOYEE_NAME, EMPLOYEE_ID
You can also use partition by clause and select whichever columns you want to see irrespective of the columns you are using for aggregation.
A very short and simple explanation here - Oracle "Partition By" Keyword