Fetch result from two different tables and show as single result - sql

I have two tables named document and levelnew
Table1: document:
document(mno,dept,step1_userid,step2_userid,step3_userid)
select * from document where step3_userid is not null;
Table2: levelnew:
levelnew(dept,level2_id,level3_id)
select dept from levelnew where level3_id='40016022';
Can i join the tables and get answer as single result?

Make a join on Dept.
select A.*,B.dept from document A inner join levelnew B On
A.dept = B.dept
Where step3_userid is not null and level3_id='40016022'

Below query will give the required result :-
select
D.*,L.dept
from
document D
inner join
levelnew L on D.dept=L.dept
where L.level3_id='40016022' AND D.step3_userid is not null;

Related

SQL column added twice during INNER JOIN

I am trying to join two tables from a database; energyImport and sunAlt.
energyImport has three columns: timestamp_id, energy, duration.
sunAlt has two columns: timestamp_id, altitude
I am doing an inner join on these two tables using the SQL:
SELECT *
FROM energyImport
INNER JOIN sunAz ON sunAz.timestamp_id = energyImport.timestamp_id;
The output from this is:
timestamp_id,duration,energy,timestamp_id,altitude
1601769600,1800,81310,1601769600,0.0
1601771400,1800,78915,1601771400,0.0
1601773200,1800,78305,1601773200,0.0
The problem is that the timestamp_id column is repeated. How can I join these columns and only include the first timestamp_id?
Replace the * with
energyImport.timestamp_id,energyImport.duration, energyImport.energy,
sunAz.altitude
Either you specify the columns that you want in the results:
SELECT e.timestamp_id, e.duration, e.energy, s.altitude
FROM energyImport e INNER JOIN sunAz s
ON s.timestamp_id = e.timestamp_id;
Or, use NATURAL instead of INNER join, so that the join is based on the columns(s) with the same names of the 2 tables (if this fits your requirement), because NATURAL join returns only 1 of each pair of these columns:
SELECT *
FROM energyImport NATURAL JOIN sunAz;
See the demo.

Remove duplicates from result in sql

i have following sql in java project:
select distinct * from drivers inner join licenses on drivers.user_id=licenses.issuer_id
inner join users on drivers.user_id=users.id
where (licenses.state='ISSUED' or drivers.status='WAITING')
and users.is_deleted=false
And result i database looks like this:
And i would like to get only one result instead of two duplicated results.
How can i do that?
Solution 1 - That's Because one of data has duplicate value write distinct keyword with only column you want like this
Select distinct id, distinct creation_date, distinct modification_date from
YourTable
Solution 2 - apply distinct only on ID and once you get id you can get all data using in query
select * from yourtable where id in (select distinct id from drivers inner join
licenses
on drivers.user_id=licenses.issuer_id
inner join users on drivers.user_id=users.id
where (licenses.state='ISSUED' or drivers.status='WAITING')
and users.is_deleted=false )
Enum fields name on select, using COALESCE for fields which value is null.
usually you dont query distinct with * (all columns), because it means if one column has the same value but the rest isn't, it will be treated as a different rows. so you have to distinct only the column you want to, then get the data
I suspect that you want left joins like this:
select *
from users u left join
drivers d
on d.user_id = u.id and d.status = 'WAITING' left join
licenses l
on d.user_id = l.issuer_id and l.state = 'ISSUED'
where u.is_deleted = false and
(d.user_id is not null or l.issuer_id is not null);

Get all columns from other tables with a distinct

I am doing a distinct to filter by 2 columns, but I need it to bring me all the columns of the query, in this case it only brings me "idMes" and "idAnio", I need it to show me the other columns as well.
How could I do it?
this is my sentence:
SELECT DISTINCT e.idMes, e.idAnio FROM expensas as e INNER JOIN anios as a on e.idAnio = a.idAnio INNER JOIN meses as m on e.idMes = m.idMes;
Select * gives you all columns

Query to Id referencing to an 'Id' to the same table

I have a table AMZ_EMPLOYEE_DETAILS with Employee Id, Employee Name and Supervisor1 and Supervisor 2. The Supervisors are employee itself but represented by their name.
My task was to replace the Supervisor names with their Ids.
I have used the following query to obtain the solution but it uses sub-queries and the query does look optimized. For this I have also made a dimension table with all the Id and Employee Names ,table AMZ_EMPLOYEE
SELECT S1.ID, S1.EMP_NAME, S1.SUPER_1_NEW, ZZ.ID AS SUPER_2_NEW
FROM
(SELECT A.ID,A.EMP_NAME,A.SUPER_1,A.SUPER_2,Z.ID AS SUPER_1_NEW
FROM AMZ_EMPLOYEE_DETAILS A
LEFT JOIN
AMZ_EMPLOYEE Z
ON A.SUPER_1 = Z.EMP_NAME ) S1
LEFT JOIN
AMZ_EMPLOYEE ZZ
ON S1.SUPER_2 = ZZ.EMP_NAME
ORDER BY 1
The following is the expected output.
The logic to use two self-LEFT JOIN is correct. You don't need to use subqueries though. Consider:
SELECT a.id, a.emp_name, a1.id, a2.id
FROM amz_employee_details a
LEFT JOIN amz_employee a1 ON a1.emp_name = a.super_1
LEFT JOIN amz_employee a2 ON a2.emp_name = a.super_2

Inner join query

Please go thourgh Attached Image where i descirbed my scenario:
I want SQL Join query.
Have a look at something like
SELECT *
FROM Orders o
WHERE EXISTS (
SELECT 1
FROM OrderBooks ob INNER JOIN
Books b ON ob.BookID = b.BookID
WHERE o.OrderID = ob.OrderID
AND b.IsBook = #IsBook
)
The query will return all orders based on the given criteria.
So, what it does is, when #IsBook = 1 it will return all Orders where there exists 1 or more entries linked to this order that are Books. And if #IsBook = 0 it will return all Orders where there exists 1 or more entries linked to this order that are not Books.
Inner join is a method that is used to combine two or more tables together on base of common field from both tables. the both keys must be of same type and of length in regardless of name.
here is an example,
Table1
id Name Sex
1 Akash Male
2 Kedar Male
similarly another table
Table2
id Address Number
1 Nadipur 18281794
2 Pokhara 54689712
Now we can perform inner join operation using the following Sql statements
select A.id, A.Name, B.Address, B.Number from Table1 A
INNER JOIN Table2 B
ON A.id = B.id
Now the above query gives one to one relation details.