I am running my Oracle query here but it's not working but the same query is working in SQL Server
Here is my query:
SELECT d.dept_code,
d.dept_name,
d.dept_desc,
e.comp_name
FROM dept_master d
inner join comp_master e
ON d.comp_id = e.comp_id
where in dept_master.comp_id value is the same as in Dept_Master table.
The reason you are not getting any result is mainly because of the data
do this check to see if data is available in the tables
select * from dept_master;
select * from comp_master;
and see if both tables have any matching rows, i.e.; at least 1 row has same comp_id in both tables
I hope you will find the answer after doing this exercise
Is comp_id a character field? In that case define it as VARCHAR2 in Oracle. or try trim(d.comp_id) = trim(e.comp_id)
See a demonstration in SQL Fiddle.
Related
i tried to figure out how to do the number 7 query on Helpdesk DB on SQLZOO (medium questions) but I can't. Not even using subquery or not exist statement. Here is the link (n. 7).
http://sqlzoo.net/wiki/Helpdesk_Medium_Questions
select first_name, last_name
from Caller a
left join Issue b
on a.Caller_id = b.Caller_id
where Call_date is null
This query says -
select ALL rows from Caller table regardless of them having a join
select ONLY rows from Issue where they have a join
Therefore, any row that returns from the Caller table that has not made a call will have a null value for Call_date. The where clause says only give me those rows.
You need to use LEFT JOIN on the Issue table and then add a filter where a field in the Issue table is null
SELECT ca.First_name, ca.Last_name
FROM Caller ca
LEFT JOIN Issue i ON ca.Caller_id = i.Caller_id
WHERE i.Caller_id is null
I would like to JOIN 2 databases.
1 database is keyword_data (keyword mapping)
1 database is filled with Google rankings and other metrics
Somehow I cannot JOIN these two databases.
Some context:
DATA SET NAME: visibility
TABLE 1
keyword_data
VALUES
keyword
universe
category
search_volume
cpc
DATA SET NAME: visibility
TABLE 2
results
VALUES
Date
Keyword
Website
Position
In order to receive ranking data by date I wrote the following SQL line.
SELECT Date, Position, Website FROM `visibility.results` Keyword INNER
JOIN `visibility.keyword_data` keyword ON `visibility.results` Keyword
= `visibility.keyword_data` keyword GROUP BY Date;
(besides that, 100 other lines with no success ;-) )
I am using Google BigQuery for this with standard SQL (unchecked Legacy SQL).
How can I JOIN those 2 data tables?
How familiar are you with SQL? I think you're using aliases wrong, something like this should work
SELECT r.Date, r.Position, r.Website
FROM `visibility.results` AS r
INNER JOIN `visibility.keyword_data` AS k
ON r.Keyword = k.keyword
GROUP BY DATE
First of all i have never worked with Google big query but there is a couple of things wrong in my opinion with this query.
To start with you join tables by including the name of the table then you provide the key that the tables are joined by. Also if you don't use aggregate functions (MIN/MAX etc.) in your select statement you must include all values in the group by clause as well. In reference I can provide you a solution that would work if you would of used Microsoft SQL Server if that would be of any help because if you reference here the syntax is quite similar.
SELECT results.Date AS DATE,
,results.Position AS POSITION
,results.Website AS WEBSITE
FROM visibility.dbo.keyword_data AS keyword_data
INNER JOIN visibility.dbo.results AS results
ON results.keyword = keyword_data.keyword
GROUP BY results.Date
,results.Position
,results.Website
I migrated database from oracle to Sql server using sqlmigration(SSMA) tool.
After migration, report say 100% successfully migrated but the row counts are more than oracle in Sql Server. I cant figure out why its showing more row counts than oracle. Please Help
You can check row counts of all the tables in sql server using below query
SELECT
t.name, i.rows
FROM sys.tables t
INNER JOIN sysindexes i
ON t.object_id = i.id
WHERE i.indid < 2
Similarly for oracle you can use this query
Get counts of all tables in a schema
and you can compare
perform row count like ->
-- Source
select count(1) from table_source;
-- Target
select count(1) from table_target;
I am having some problem when trying to perform a SQLite query to get the records which does not exist from another table. Basically I have two database tables:
My exercise table stored all the exercises available whereas the bookedExercise table store the exercises booked by each users. What I am trying to do is, for example if the exercise does exist in the bookedExercise, it should be filtered out.
Here is the SQLite query which I used:
SELECT exercise.exerciseID, exercise.exerciseType, exercise.amout FROM exercise LEFT JOIN bookedExercise WHERE exercise.exerciseID = bookedExercise.exerciseID AND bookedExercise.exerciseID IS NULL
However, it returned me empty records. Any ideas?
Thanks in advance.
If you're fine with not using joins you could use
SELECT * FROM exercise WHERE exerciseID not in (SELECT exerciseID FROM bookedExercise)
When you are using LEFT JOIN, you must put the join condition into the ON clause:
SELECT exercise.exerciseID,
exercise.exerciseType,
exercise.amout
FROM exercise /* !! */
LEFT JOIN bookedExercise ON exercise.exerciseID = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL
Your SQL looked okay... I think the problem might be you have a datatype mismatch. You have exercise ID as an integer in one table and text in another.
Also, if you have huge data volumes, you may want to consider an anti-join:
select
e.*
from
exercise e
where not exists (
select 1
from bookedExercise be
where
e.excerciseId = be.exerciseID
)
-- edit --
On second glance, your SQL was not okay. You had your join condition in the where clause. This little change would fix your existing SQL:
SELECT exercise.excerciseId , exercise.exerciseType , exercise.amout
FROM exercise LEFT JOIN bookedExercise on
exercise.excerciseId = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL
We have observed that there seems to be a maximum number of ids/variables which one can pass in the IN clause of SQL as comma separated values. To avoid this we are storing all the ids in a table and doing a SELECT within the IN clause. This however means extra database operations to store and retrieve ids. Is there any other way to use IN without SELECT?
Regards,
Sameer
In SQL Server 2008 you can declare a table variable and pass it to your query from the client or between the procedures.
For a modest number of values I would not have thought an IN (SELECT ..) would be that expensive on any rdbms.
You could INNER JOIN you IDs table or just break down the INs:
WHERE X IN (123,456 ...)
OR X IN (789,987 ...)
...
Agree with Alex
Instead of
Select * From TableA Where ID IN (Select ID from IDTable)
Use
Select * From TableA
INNER JOIN IDTable ON TableA.ID = IDTable.ID
The join will automatically filter the IDs for you.
If you put it in atable why are you still using the in clause, why not just join to the table?