I'm Stuck on this Oracle Query ( Shows no data when run ) - sql

so I'm trying to answer this query in my oracle database . I'm quite new to SQL.
My Query is to List the full details of the cinemas managed by the employees with the employee number 52 and 55.
I have the test data for this in both the employee and Cinema table which correlates with one another.
However, when I run the statement it just shows the selected column names with no data . And when I run the script it says no rows selected. ( Even though I've commited my changes to my test data in both tables ).
Below is the code I've used.
SELECT Cinema.Cinema_no , Cinema.Cinema_Name , Cinema.Location , Cinema.Managerempno
FROM CINEMA
INNER JOIN employee ON Cinema.Cinema_no = employee.emp_no
WHERE Cinema.Managerempno = '52' AND Cinema.Managerempno = '55' ;
Please let me know if I've gone completely wrong with this. Or whether I need to change something within it. Thank you

Use IN:
SELECT c.Cinema_no, c.Cinema_Name, c.Location, c.Managerempno
FROM CINEMA c
WHERE c.Managerempno IN (52, 55);
Notes:
You don't need the JOIN. The employee number is in the Managerempno column.
Table aliases make queries easier to write and to read.
Empno is -- presumably -- a number. So, don't use single quotes around the constants.

Related

SQL will output my query only if I alias my columns in my select statement... Not sure why

Alright this is my curiosity, my question is why...
First this is the ERD to understand the tables and connections...
Now my Query is supposed to "Provide a table that provides the region for each sales_rep along with their associated accounts. Your final table should include three columns: the region name, the sales rep name, and the account name. Sort the accounts alphabetically (A-Z) according to account name"
I created this query:
select acc.name , reg.name , srep.name
from region reg
join sales_reps srep
on srep.region_id = reg.id
join accounts as acc
on acc.sales_rep_id = srep.id
order by 1;
This resulted in outputting only the first column of my query like so:
But it's not until I add aliases to each of my columns that the output is generated:
select acc.name account_name
, reg.name region_name
, srep.name sales_rep_name
from region reg
join sales_reps srep
on srep.region_id = reg.id
join accounts as acc
on acc.sales_rep_id = srep.id
order by 1;
Output:
Can anyone tell me why that is ? Thank you !!!!
IMPORTANT CONTEXT INFO:This is using MODE and it's part of the SQL Course on Udacity COURSE Joins Section step 12
You never told us what your actual SQL engine is (e.g. MySQL, SQL Server, Oracle, etc.). But this doesn't really even matter, because on most engines your first select would in fact have generated a result set with three columns all called name. You may confirm this behavior by just running your query directly against the actual SQL database.
What is likely happening here is that the Udacity SQL tool has inserted some layer in between the SQL database and the UI, the latter whose output you have been posting in your question. This additional layer is, for some reason, resolving duplicate column names in the result set by keeping only one of them. The workaround, as you have already figured out, is to just use unique aliases.

When using JOIN feature in Oracle, I have some question about when I am using extra ALIAS in this code

Before start, Sorry some of results and datas are written in Korean.
Here is a code that I currently am looking on.
SELECT S.*, D.DNAME
FROM STUDENT S, DEPARTMENT D
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
FROM STUDENT GROUP BY SUBSTR(JUMIN,7,1))
AND S.DEPTNO1 = D.DEPTNO;
And here is the DEPARTMENT data.
And this is the STUDENT data.
I got a result as I want. But I have some questions when I change this part of the code
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)
into this one
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT
S.SUBSTR(JUMIN,7,1),MAX(S.WEIGHT)
What I do is simply put S infront of JUMIN and WEIGHT in line3.
But when I do this it shows me the whole data.
I thought JOIN ALIAS (which are S,D in this code) is used as way show that I have two tables to use that is labeled with S and D. S means this data is in STUDENT and D is in DEPARTMENT.
But I think I get it in a wrong way.
Anyway I have no idea how this result is come out.
This one is referring to your main table not the subquery table.
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT
S.SUBSTR(JUMIN,7,1),MAX(S.WEIGHT)
Your original query is already correct. In which you already have a new result set based on your aggregation.
WHERE (SUBSTR(S.JUMIN,7,1),S.WEIGHT) IN (SELECT SUBSTR(JUMIN,7,1),MAX(WEIGHT)

Is there a way to select automatically the row pointed by an FK on a given table?

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')

Counting database items with join

Using ORMLite, I want to count the number of database items that fit certain criteria. A simplified version of my database is as follows:
Employee Table:
employeeId
departmentId
Department Table:
departmentId
Salary Table:
employeeId
payGrade
Suppose I want to count the number of employees from a subset of departments that are at certain pay grade. When I try something like the following, I get an exception reporting that only one select column is allowed.
salaryQB.selectColumns(salaryQB.EMPLOYEE_ID);
salaryQB.where().eq(salaryQB.PAY_GRADE, 12);
employeeQB.where.in(Employee.DEPARTMENT_ID, departmentList)
.and().in(employeeQB.EMPLOYEE_ID, salaryQB);
employeeQB.setCountOf(true);
count = dao.countOf(employeeQB.prepare());
But code like this returns the following error:
RuntimeException (java.sql.SQLException: Inner query must have only 1
select column specified instead of 2)
Is there any way to do this short of writing a raw SQL query?
Hrm. I can't see any problems with your code #Jeff. I've improved the exception message in ORMLite to show the actual field names for the future but that won't help you right now:
Inner query must have only 1 select column specified instead of 2: [id, foreign]
The message is [obviously] trying to tell you that you have specified more than one selectColumns(...) in the salaryQB inner QueryBuilder. But you seem to be only selecting one column here:
salaryQB.selectColumns(salaryQB.EMPLOYEE_ID);
I don't see where the salaryQB is defined but maybe there is some more code somewhere else that also is using selectColumns? I've tried to reproduce the error in the testInnerCountOf() method towards the bottom of the QueryBuilderTest unit test but it seems to work fine.
If you can reproduce this in a unit test or if you can see how my unit test differs from your config then let me know.
Edit:
As of version 4.22 (from 9/2012), ORMLite now supports simple JOIN statements. So your query can be simplified to:
salaryQB.where().eq(salaryQB.PAY_GRADE, 12);
employeeQB.join(salaryQB);
employeeQB.where().in(Employee.DEPARTMENT_ID, departmentList);
employeeQB.setCountOf(true);
count = dao.countOf(employeeQB.prepare());

Simple INNER JOIN Query Returns No Value - Where Did i Go Wrong?

The query below fetches the wageTypeID based on the current active hiring info record and salary record.
SELECT wt.wageTypeID
FROM TimeSheet t
INNER JOIN Employee e ON e.employeeID = t.employeeID
INNER JOIN Salary s ON s.salaryID = t.salaryID
INNER JOIN Wage w ON w.wageID = s.wageID
INNER JOIN EmpHiringInfo ehf ON ehf.EmpHiringInfoID = s.EmpHiringInfoID
INNER JOIN WageType wt ON wt.wageTypeID = w.wageTypeID
WHERE ehf.employeeID = 300
AND ehf.isActive = 1
AND s.isActive = 1
The above query should return the value 15! I wonder where did i go wrong?
Here is the schema of the joined tables:
TimeSheet:
timeSheetID
employeeID - 300
salaryID
.
.
Salary:
salaryID
EmpHiringInfoID
wageID
isActive - true
.
.
WageType:
wageTypeID - 15
wageTypeTitle - Hourly
Wage:
wageID
wageTypeID - 15
wageAmount - $11
EmpHiringInfo:
EmpHiringInfoID
employeeID
isActive - true
.
.
The isActive in Salary is only and only True for one record. Same thing applied to
EmpHiringInfo.
Try commenting out one join and see if you get any results back, if not comment out another and so on until you start seeing results. The last one you commented out might give you a clue as to why its failing.
Without an actual data dump (and the create table statements), we can't exactly say because we can't see your data. So here's how to approach the situation so you can find out where the issue is:
Scale back the SELECT statement to have no JOINs--you want to make sure the base table returns the record(s) you need before going any further.
Add a JOIN, one at a time. Each JOIN you add, you need to check the resultset to see that the rows you're expecting are still visible. If a JOIN has more than one criteria (IE: ehf), then again--incrementally add criteria while checking every time.
Eventually, the criteria that is causing the issue will become obvious and so you'll have to review how to handle the situation. It's possible more than one JOIN is causing troubles, and you might not see the others until after one is addressed.
I recommend restructuring your query to use the table you're selecting the most data from in the FROM clause. In this case, that means:
SELECT wt.wageTypeID
FROM WAGETYPE wt
That will make adding JOINs easier.
Look at the actual execution plan. Mouse over the arrows. Look and see where the number of rows returned from a JOIN becomes zero.
Rebuild the Select SQL a join at a time, starting with EmpHiringInfo and Salary, since those are the core ones referenced in your where clause.