SQL Join Not Returnig Desired Results - sql

I'm having a little trouble getting a SQL query working to give me the desired results. I have three tables. The SQL query should return all records with an employee name, along with their corresponding department name, and a division should it exist. If they do not, they should still display the employee with a Null division. Here is a link to my SQL Fiddle...
http://sqlfiddle.com/#!3/76fbb/1/0

Use LEFT OUTER JOIN instead of RIGHT OUTER JOIN

use a LEFTjoin instead of a RIGHTjoin.

Related

Checking one sub query to contain results of the other in T-SQL

I am trying to write a query that should perform join (or where clause) using the following criteria:
Subquery of left table should contain results of subquery from the right table (results also should be grouped).
sql script creating tables with data
Exact criteria is the following:
Select clients who:
For each Question.Id in [ClientSegment]'s questionIds
Should be at least one (equivalent on Any in LINQ) ClientAnswers.AnswerId from those in [ClientSegmentAnswers]
How I can achieve that without using for loops (cursors) in a single query?
UPD
Added script for creating tables and data
Expected result
Query that selects Bob as only matching consumers for ClientSegment with id = 1;
SQL JOINS will help for achieve this in single query,
E.g :
select
c.name,
c.id,
...
from Question as que
left join ClientSegment as cs on que.id=cs.id
left join ClientSegmentAnswers as csans on csans.ClientSegmentID=cs.id
left join Answer as ans on ans.id=csans.answerid
left join ClientAnswer as ca on ca.answerid=ans.answerid
left join Client as c on c.id=ca.ClientID
where ca.ClientID is not null
This query will return only clientname who are answering atleast one question also you can modify the query for your need.

How to Select Specific data on query with RIGHT JOIN statement?

I am joining 2 tables using RIGHT JOIN statement. I used below query and it works good. However it still display all data whenever I tried to select specific user
SELECT TBLNOTIFICATIONS.NOTIFICATION_ID, TBLNOTIFICATIONS.NOTIFICATION_TYPE, FILENAMES_LIST.LOCATION_FILENAME, TBLNOTIFICATIONS.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS
RIGHT JOIN FILENAMES_LIST
ON TBLNOTIFICATIONS.NOTIFICATION_ID=FILENAMES_LIST.NOTIFICATION_ID
WHERE TBLNOTIFICATIONS.USER_ID='JCON'
What should I do to select data from specific user?
Thanks in advance.
You are filtering on the left table, so all the data of the right table will still be shown.
It is probably enough to change the query to a LEFT JOIN to get the results you want.
Besides that, you can use aliases to make your query more readable, like so:
SELECT tn.NOTIFICATION_ID, tn.NOTIFICATION_TYPE, fl.LOCATION_FILENAME, tn.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS AS tn
LEFT JOIN FILENAMES_LIST AS fl
ON tn.NOTIFICATION_ID = fl.NOTIFICATION_ID
WHERE tn.USER_ID='JCON'

How does Subquery in select statement work in oracle

I have looked all over for an explanation, to how does the subquery in a select statement work and still I cannot grasp the concept because of very vague explanations.
I would like to know how do you use a subquery in a select statement in oracle and what exactly does it output.
For example, if i had a query that wanted to display the names of employees and the number of profiles they manage from these tables
Employee(EmpName, EmpId)
Profile(ProfileId, ..., EmpId)
how do I use the subquery?
I was thinking a subquery is needed in the select statement to implement the group by function to count the number of profiles being managed for each employee, but I am not too sure.
It's simple-
SELECT empname,
empid,
(SELECT COUNT (profileid)
FROM profile
WHERE profile.empid = employee.empid)
AS number_of_profiles
FROM employee;
It is even simpler when you use a table join like this:
SELECT e.empname, e.empid, COUNT (p.profileid) AS number_of_profiles
FROM employee e LEFT JOIN profile p ON e.empid = p.empid
GROUP BY e.empname, e.empid;
Explanation for the subquery:
Essentially, a subquery in a select gets a scalar value and passes it to the main query. A subquery in select is not allowed to pass more than one row and more than one column, which is a restriction. Here, we are passing a count to the main query, which, as we know, would always be only a number- a scalar value. If a value is not found, the subquery returns null to the main query. Moreover, a subquery can access columns from the from clause of the main query, as shown in my query where employee.empid is passed from the outer query to the inner query.
Edit:
When you use a subquery in a select clause, Oracle essentially treats it as a left join (you can see this in the explain plan for your query), with the cardinality of the rows being just one on the right for every row in the left.
Explanation for the left join
A left join is very handy, especially when you want to replace the select subquery due to its restrictions. There are no restrictions here on the number of rows of the tables in either side of the LEFT JOIN keyword.
For more information read Oracle Docs on subqueries and left join or left outer join.
In the Oracle RDBMS, it is possible to use a multi-row subquery in the select clause as long as the (sub-)output is encapsulated as a collection. In particular, a multi-row select clause subquery can output each of its rows as an xmlelement that is encapsulated in an xmlforest.

Sql join shows null value

I have 2 sql tables mtblSite_Lavel_Budget and mtblExpenditure_Site_Lavel. I want to join them, so I wrote the query below, but one column always give null result although there are values in that field.
Query is as below
select mtblExpenditure_Site_Lavel.SFTI_Id,
mtblExpenditure_Site_Lavel.Site_Lavel_Interventions,
mtblExpenditure_Site_Lavel.Sub_Site_Lavel_Interventions,
mtblExpenditure_Site_Lavel.Bill_No, mtblExpenditure_Site_Lavel.Date_Of_Bill,
mtblExpenditure_Site_Lavel.Eligible_Exp,
mtblExpenditure_Site_Lavel.Non_Eligible_Exp,
mtblExpenditure_Site_Lavel.Total,
mtblExpenditure_Site_Lavel.Physical_Progress,
mtblSite_Lavel_Budget.Sanction_Amount_DPR
from mtblExpenditure_Site_Lavel
left join mtblSite_Lavel_Budget
on mtblExpenditure_Site_Lavel.Sub_Site_Lavel_Interventions= mtblSite_Lavel_Budget.Sub_Site_Lavel_Interventions
WHERE mtblExpenditure_Site_Lavel.SFTI_Id =13
ORDER BY Date_Of_Bill desc
and the result is as below
Please advise regarding the issue.
Try this
from mtblExpenditure_Site_Lavel left join mtblSite_Lavel_Budget on
mtblExpenditure_Site_Lavel.sfti_id = mtblSite_Lavel_Budget.sfti_id
I think its confused becouse you are joining on a string column its always best to join on the id where posible.

Need help forming SQLite join query

I've been able to make a query using MS Access 2010 that does just what I want but I am having trouble doing so in SQLite3. Here is the Access SQL
SELECT pubacc_lo.*
FROM pubacc_en
LEFT JOIN pubacc_lo ON pubacc_en.call_sign = pubacc_lo.call_sign;
Basically it selects all of the columns in the pubacc_lo table and the rows where the call_sign fields are equal between the tables. This does not select any of the pubacc_en data into the final query in MS Access.
Playing around in SQLite 3, the closest I've gotten was
SELECT * FROM PUBACC_LO, PUBACC_EN WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
But this statement selects all of the data in the EN table along with the LO table (cross join?). I've tried some left outer joins but haven't had any luck. Any tips would be appreciated!
You should be able to use the same query as you got from Access. SQLite3 does support left outer joins.
As for your query, if you only want the fields from the LO table, then ask for that in your SELECT clause like this:
SELECT PUBACC_LO.*
FROM PUBACC_LO, PUBACC_EN
WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
but the problem here is that it will only return call_signs with entries in both tables, while the outer join from access will return all rows from PUBACC_EN irrespective of whether there is a corresponding PUBACC_LO entry..
SELECT pubacc_lo.* FROM PUBACC_LO, PUBACC_EN WHERE UBACC_en.call_sign=PUBACC_LO.call_sign
If you only want to select pubbac_lo's field, this is what you can use.