I am running a query on two different tables:-
select * from MessagesTable INNER JOIN ErrorTable ON MessagesTable.msgId = ErrorTable.msgId.
Is there an another way to write this query without using JOIN?
If you would not use join in query you can write sub query for this.
Below query for SQL DATABASE-
select *
from MessagesTable
where MessagesTable.msgId in (select ErrorTable.msgId from ErrorTable)
I don't know why do you want to do it but YES you can do it as long as you want data from only MessagesTable. Its called sub query.
select * from MessagesTable WHERE msgId IN (select msgId from ErrorTable)
Yes, you can use this query in place of join for particular column
select * from MessagesTable,ErrorTable where MessagesTable.msgId =ErrorTable.msgId
Related
I am making a subquery but I am getting a strange error
The column 'RealEstateID' was specified multiple times for 'NotSold'.
here is my code
SELECT *
FROM
(SELECT *
FROM RealEstatesInfo AS REI
LEFT JOIN Purchases AS P
ON P.RealEstateID=REI.RealEstateID
WHERE DateBought IS NULL) AS NotSold
INNER JOIN OwnerEstate AS OE
ON OE.RealEstateID=NotSold.RealEstateID
It's on SQL server by the way.
That's because there will be 2 realestiteids in your subquery. You need to change it to explicitly list the columns from both table and only include 1 realestateid. It doesn't matter which as you use it for your join.
If you're very Lazy you can select rei.* and only name the p cols apart from realestateid.
Btw select * is probably never a good idea in sub queries or derived tables or ctes.
I have two tables 1. tdppackages and 2. tpdstop and I do a SQL SELECT INNER JOIN to create a TableAdapter with some info from both and I want to NOT add duplicate records. Here is my SQL Statement:
SELECT tdppackages.trackno,
tdppackages.shpmentno,
tpdstop.custname,
tpdstop.address,
tpdstop.city,
tdppackages.amtdue,
tpdstop.pkgs,
tpdstop.ndx
FROM tpdstop
INNER JOIN tdppackages ON tpdstop.ndx = tdppackages.stopkey
Change SELECT to SELECT DISTINCT is the fastest way.
I think you would be having composite key which you should include in on clause.
like
INNER JOIN tdppackages ON tpdstop.ndx = tdppackages.stopkey
And tpdstop.col2 = tdppachages.col2
Do LEFT JOIN instead. Knowing the differences of each join would be very helpful moving forward.
I am trying to tune SQLs which have NOT EXISTS clause in the queries.My database is Netezza.I tried replacing NOT EXISTS with NOT IN and looked at the query plans.Both are looking similar in execution times.Can someone help me regarding this?I am trying to tune some SQL queries.Thanks in advance.
SELECT ETL_PRCS_DT, COUNT (*) TOTAL_PRGM_HOLD_DUE_TO_STATION
FROM DEV_AM_EDS_1..AM_HOLD_TV_PROGRAM_INSTANCE D1
WHERE NOT EXISTS (
SELECT *
FROM DEV_AM_EDS_1..AM_STATION
WHERE D1.STN_ID = STN_ID
)
GROUP BY ETL_PRCS_DT;
You can try a JOIN:
SELECT ETL_PRCS_DT, COUNT (*) TOTAL_PRGM_HOLD_DUE_TO_STATION
FROM DEV_AM_EDS_1..AM_HOLD_TV_PROGRAM_INSTANCE D1
LEFT JOIN DEV_AM_EDS_1..AM_STATION TAB2 ON D1.STN_ID = TAB2.STN_ID
WHERE TAB2.STN_ID IS NULL
Try to compare the execution plans. The JOIN might produce the same you already have.
You can try a join, but you sometimes need to be careful. If the join key is not unique in the second table, then you might end up with multiple rows. The following query takes care of this:
SELECT ETL_PRCS_DT,
COUNT (*) TOTAL_PRGM_HOLD_DUE_TO_STATION
FROM DEV_AM_EDS_1..AM_HOLD_TV_PROGRAM_INSTANCE D1
left outer join
(
select distinct STN_ID
from DEV_AM_EDS_1..AM_STATION ams
) ams
on d1.STN_ID = ams.STN_ID
WHERE ams.STN_ID is NULL
I'm trying to construct an SQL query that selects data from both tables but it doesn't seem to be working:
SELECT DISTINCT name,$price
FROM room
WHERE capacity>=$partySize
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date';
What am I doing wrong?
Move the WHERE clause down to the bottom:
SELECT DISTINCT name,$price
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize
Definitely read some more SQL tutorials. You cannot use two WHERE conditions in one query:
SELECT DISTINCT name,'$price'
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>='$partySize';
Also, variables in the query should be quoted.
try
SELECT DISTINCT r.name,$price
FROM room r, room_booking b
WHERE r.capacity>=$partySize
and r.room_id=b.room_id
and r.date_booked<>'$us_date';
What's $price? is it a value? if variable try r.$price
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.