[Solved] SELECT....NOT IN ()

Discuss the database features
Post Reply
dreamquartz
Posts: 881
Joined: Mon May 30, 2011 4:02 am

[Solved] SELECT....NOT IN ()

Post by dreamquartz »

Hello All,

Can someone guide me in the right direction with the code below?
I need to have 2 criteria verified at the same time.

Code: Select all

SELECT
	 "vQuery8"."CompanyID",
	 "vQuery8"."CertificateQualificationID"
FROM
	 "vQuery8"
WHERE
	 "CompanyID" AND
	 "CertificateQualificationID"
		 NOT IN
			(
				 SELECT
					 *
				 FROM
					 "vQuery9"
			 )
Thanks in advance,

Dream
Last edited by dreamquartz on Fri Jan 18, 2019 7:21 pm, edited 2 times in total.
LO 7.x, HSQLDB 2.7.x & Ubuntu 22.04 LTS.
User avatar
robleyd
Moderator
Posts: 5079
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: SELECT....NOT IN ()

Post by robleyd »

Did you try

Code: Select all

    SELECT
        "vQuery8"."CompanyID",
        "vQuery8"."CertificateQualificationID"
    FROM
        "vQuery8"
    WHERE
        "CompanyID" NOT IN (SELECT  * FROM "vQuery9" )
       AND
        "CertificateQualificationID" NOT IN (SELECT  * FROM "vQuery9" )
I'd suspect that WHERE "CompanyID" would select where the value of CompanyId was TRUE, then AND with the next part of the WHERE clause.

Edit: and perhaps select the specific field you want to match in the NOT IN queries, rather than a wildcard.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
FJCC
Moderator
Posts: 9271
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: SELECT....NOT IN ()

Post by FJCC »

robleyd wrote: and perhaps select the specific field you want to match in the NOT IN queries, rather than a wildcard.
Yes, the sub query must return only one column.

Code: Select all

WHERE
        "CompanyID" NOT IN (SELECT  "CompanyID" FROM "vQuery9" )
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: SELECT....NOT IN ()

Post by Villeroy »

Code: Select all

SELECT
    "vQuery8"."CompanyID",
    "vQuery8"."CertificateQualificationID"
FROM "vQuery8" LEFT JOIN "vQuery9" 
    ON "vQuery8"."CompanyID"="vQuery9"."CompanyID"
        AND "vQuery8"."CertificateQualificationID"="vQuery9"."CertificateQualificationID"
WHERE "vQuery9"."CompanyID" IS NULL AND "vQuery9"."CertificateQualificationID" IS NULL
assuming that the corresponding column names in vQuery9 are the same as in vQuery8
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dreamquartz
Posts: 881
Joined: Mon May 30, 2011 4:02 am

Re: SELECT....NOT IN ()

Post by dreamquartz »

Villeroy wrote:

Code: Select all

SELECT
    "vQuery8"."CompanyID",
    "vQuery8"."CertificateQualificationID"
FROM "vQuery8" LEFT JOIN "vQuery9" 
    ON "vQuery8"."CompanyID"="vQuery9"."CompanyID"
        AND "vQuery8"."CertificateQualificationID"="vQuery9"."CertificateQualificationID"
WHERE "vQuery9"."CompanyID" IS NULL AND "vQuery9"."CertificateQualificationID" IS NULL
assuming that the corresponding column names in vQuery9 are the same as in vQuery8
Yup,
This is the answer.

Tried everything with NOT IN ().
Never thought about LEFT JOIN.

Thank you so much,

Dream
LO 7.x, HSQLDB 2.7.x & Ubuntu 22.04 LTS.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [SOLVED] SELECT....NOT IN ()

Post by Villeroy »

NOT IN (SELECT...) should work as well, possibly a little bit slower. Your mistake was that you try to compare one field of one record set with all fields of another record set.

Code: Select all

SELECT columns
FROM A, B
WHERE A.X = B.Y
... is perfectly equivalent to ...

Code: Select all

SELECT columns
FROM A INNER JOIN B 
  ON A.X = B.Y
The INNER keyword is optional. A JOIN B means the same.

Code: Select all

SELECT columns
FROM A LEFT OUTER JOIN B 
  ON A.X = B.Y
... returns the same records as the inner join plus all the non-matching records of the left table with Null values for the columns of the right table.
The OUTER keyword is optional. A LEFT JOIN B means the same.

Code: Select all

SELECT columns
FROM A RIGHT OUTER JOIN B 
  ON A.X = B.Y
... returns the same records as the inner join plus all the non-matching records of the right table with Null values for the columns of the left table.
The OUTER keyword is optional. A RIGHT JOIN B means the same.

The following is not supported by HSQL 1.8

Code: Select all

SELECT columns
FROM A OUTER JOIN B 
  ON A.X = B.Y
It should return the same records as the inner join plus all the non-matching records of the right table with Null values for the columns of the right table plus all the non-matching records of the left table with Null values for the columns of the left table.

With HSQL 1.8 you get an OUTER JOIN from this:

Code: Select all

SELECT columns
FROM A LEFT OUTER JOIN B 
  ON A.X = B.Y
UNION SELECT columns
FROM A RIGHT OUTER JOIN B 
  ON A.X = B.Y
Of course you knew that already.

P.S.: Tutorial with attachment: viewtopic.php?f=83&t=96654
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dreamquartz
Posts: 881
Joined: Mon May 30, 2011 4:02 am

Re: [SOLVED] SELECT....NOT IN ()

Post by dreamquartz »

Villeroy wrote:NOT IN (SELECT...) should work as well, possibly a little bit slower. Your mistake was that you try to compare one field of one record set with all fields of another record set.

Code: Select all

SELECT columns
FROM A, B
WHERE A.X = B.Y
... is perfectly equivalent to ...

Code: Select all

SELECT columns
FROM A INNER JOIN B 
  ON A.X = B.Y
The INNER keyword is optional. A JOIN B means the same.

Code: Select all

SELECT columns
FROM A LEFT OUTER JOIN B 
  ON A.X = B.Y
... returns the same records as the inner join plus all the non-matching records of the left table with Null values for the columns of the right table.
The OUTER keyword is optional. A LEFT JOIN B means the same.

Code: Select all

SELECT columns
FROM A RIGHT OUTER JOIN B 
  ON A.X = B.Y
... returns the same records as the inner join plus all the non-matching records of the right table with Null values for the columns of the left table.
The OUTER keyword is optional. A RIGHT JOIN B means the same.

The following is not supported by HSQL 1.8

Code: Select all

SELECT columns
FROM A OUTER JOIN B 
  ON A.X = B.Y
It should return the same records as the inner join plus all the non-matching records of the right table with Null values for the columns of the right table plus all the non-matching records of the left table with Null values for the columns of the left table.

With HSQL 1.8 you get an OUTER JOIN from this:

Code: Select all

SELECT columns
FROM A LEFT OUTER JOIN B 
  ON A.X = B.Y
UNION SELECT columns
FROM A RIGHT OUTER JOIN B 
  ON A.X = B.Y
Of course you knew that already.

P.S.: Tutorial with attachment: viewtopic.php?f=83&t=96654
Thank you Master Villeroy
LO 7.x, HSQLDB 2.7.x & Ubuntu 22.04 LTS.
Post Reply