I have a method where I get a sql query as parameter. This is used in an IN clause to form a bigger query to Select/Delete data. Something like this:
SQL = "SELECT ID FROM EMPLOYEE WHERE DEPARTMENT = 'HR'"
BIG_SQL = "DELETE FROM EMPLOYEE WHERE ID IN ("+SQL+")
It was flagged by SonarQube. Is there a better way to write the above code?
As I understand the purpose of the 2 queries is to delete rows from the table.
In the table EMPLOYEE the column ID is the primary key, right?
So you could just have a DELETE statement like this:
DELETE FROM EMPLOYEE WHERE DEPARTMENT = 'HR'
There is no need to get the ids by using the condition:
WHERE DEPARTMENT = 'HR'
and then use them to delete since this condition can be applied to directly delete the rows.
Related
I am doing some testing and need to populate Employee.DepartmentId with Id column from Department table.
When I do like this, All rows in Employee table have same DepartmentId value. But I want different DepartmentId values, it is ok if there are some duplicates.
UPDATE Employee SET DepartmentId = (SELECT Id FROM Department ORDER BY RANDOM() LIMIT 1);
How can I do this?
Adding more details:
How can I write SQL query so that for each row in Employee, engine goes and fetches a random row from Department table, rather than fetch one random row from Department table and use that for every row in Employee.
This is a problem of over-active optimizers which decide to run the subquery only once.
One solution is to add a correlation clause to the outer query:
UPDATE Employee
SET DepartmentId = (SELECT d.Id
FROM Department d
WHERE employee.id is not null
ORDER BY RANDOM() LIMIT 1
);
This gets around the optimizer.
Here is a db<>fiddle.
Hi i am using oracle sqlplus on the sample HR database tables.
I have 2 tables, employees and jobs in which i have job_id in both of them.
I want to be able to enter the input of the job_ID and then it displays the first_name, last_name (which is inside employees table)
I am new to sql and learning. Thanks in advance
When you want to query data out of an SQL Database, you need the SELECT Statement. In your case, that would be:
SELECT first_name, last_name FROM Employees WHERE job_ID=:job_ID;
SELECT ... Keyword of "SELECT" Statements
firstname, lastname ... Columns you want the query to return
FROM Employees ... Keyword + Tablename you want to query on
WHERE job_ID=:job_ID ... Condition which will be used to select specific rows
:job_ID ... Your input of which job_ID you want to get
Your searched Query was the userinput mixed with the Jobs:
SELECT first_name, last_name FROM Employees JOIN Jobs ON Employees.job_ID=Jobs.job_ID WHERE Jobs.min_salary=&min_salary AND Jobs.max_salary=&max_salary;
JOIN Combines the rows of each table to one bigger table, there are many different types of them, this is just the basic one
ON Gives the JOIN the parameter to check whether this column is the same in two rows of each table. If yes, they will be merged
I am still learning the ropes of SQL so I have run into my first obstacle. I am to create an SQL query that retrieves employee.firstname, employee.lastname, dependents.depname, and dependents.birthday from the two tables employees and dependents.
I am only supposed to show an employee if he or she has a dependent.
My primary table (employee; only the first 43 rows): employee table
My secondary table (dependents): dependents table
This is what I have so far:
SELECT
employee.firstname, employee.lastname,
dependents.depname, dependents.birthday
FROM
employee
INNER JOIN
dependents ON employee.id = dependents.empid
This works fine however I run into many duplicate rows of data:
Original Query
This is not the full query result but I think it provides sufficient evidence of my problem.
I used the DISTINCT keyword with my SELECT statement, but it only retrieved a small number of my dependents.
Adding DISTINCT
Have you already any duplicates in one of the tables employee or dependents? The second result looks correct. With select distinct the database removes all duplicates from the result set.
Today while writing one of the many queries that every developer in my company write every day I stumbled upon a question.
The DBMS we are using is Sql Server 2008
Say for example I write a query like this in the usual PERSON - DEPARTMENT db example
select * from person where id = '01'
And this query returns one row:
id name fk_department
01 Joe dp_01
The question is: is there a way (maybe using an addon) to make sql server write and execute a select like this
select * from department where id = 'dp_01'
only by for example clicking with the mouse on the cell containing the fk value (dp_01 in the example query)? Or by right click and selecting something like ("Go to pointed value")?
I hope I didn't wrote something stupid or impossible by definition
Not really, but that seems like a silly thing to do. Why would you want to confuse an id with a department name?
Instead, you could arrange things so you could do:
select p.*
from person p
where department = 'dp_01';
You would do this by adding a computed column department that references a scalar function that looks up the value in the department table. You can read about computed columns here.
However, a computed column would have bad performance characteristics. In particular, it would basically require a full table scan on the person table, even if that is not appropriate.
Another solution is to create a view, v_person that has the additional columns you want. Then you would do:
select p.*
from v_person p
where department = 'dp_01';
Why can't you write yourself by saying
select * from department where id =
(select fk_department from person where id = '01')
My table IncomingLetter has a foreign key to a table Department, which has an ID and a column Short_Name.
I'm using this query to count the incoming letters assigned to a department.
SELECT COUNT(DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter
WHERE Assigned_To_Department=1;
Whereas this works I want to make a query based upon the short name and not based upon the ID.
SELECT COUNT(DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter
WHERE Assigned_To_Department.Short_Name="My Department Name";
This does not work, whereas I found examples that are using this syntax. However, it is probably important to notice, that I m using this query in MS access.
You should use
SELECT COUNT(il.DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter il
INNER JOIN Department d on d.ID = il.Assigned_To_Department
WHERE d.Short_Name="My Department Name";
The "My Department Name" text is actually stored in the Departments table, and only the number (1) is stored in the IncomingLetter table, in the field Assigned_To_Department.
Asking for Assigned_To_Department.Short_Name basically asks the number 1 to get it's Short_Name field, that does not make sense.
You need to tell the database engine two things in these scenarios:
which tables are connected - IncomingLetter and Departments in this case (the inner join part)
how they are connected - by setting their Assigned_To_Department and ID fields respecively (the on ... part