SQL - Exclude rows from SELECT statement if a certain column combination exists in that row
Since a row can only match one of those conditions at one time, you should be using OR
:
SELECT COLUMN1, COLUMN2, COLUMN3
FROM YourTable
WHERE NOT (
( COLUMN2 = 'A' AND COLUMN3 = 'B' )
OR
( COLUMN2= 'B' AND COLUMN3 = 'C' )
)
db<>fiddle here
SQL Exclude a specific column from SQL query result
How about selecting all columns from one table and one from the other?
select t1.*, t2.col
from t1 join
t2
on . . .
Exclude column from resultset if subquery returns null
The number of columns will not change depending on the resultset, so you will always have 4 columns.
What you can do is a LEFT JOIN that will fill the Consultant column with NULL when no items are a match
SELECT
e.firstName,
e.lastName,
e.department,
c.consultantName
FROM employees e
LEFT join consultants c
on e.userId = c.userId
ORDER BY department,lastName
In the case you want two different resultset, you can write two queries :
Return 3 columns only for when the consultant doesn't exist
SELECT
e.firstName,
e.lastName,
e.department,
FROM employees e
WHERE userId not in
(
select userId from consultants
)
ORDER BY department,lastName
Return 4 columns when there is a match
SELECT
e.firstName,
e.lastName,
e.department,
c.consultantName
FROM employees e
INNER JOIN consultants c
on e.userId = c.userId
ORDER BY department,lastName
Related Topics
How to Make SQL Query Result Show With 2 Decimals
Presto SQL - Converting a Date String to Date Format
Count All Records Per Day in a Specific Month
Localhost/Phpmyadmin Giving Page Not Found Error
Multiple Rows Output into Variables in MySQL
1052: Column 'Id' in Field List Is Ambiguous
Mysql - How to Use Like on Multiple Columns
Hive Explode List from Json-String
How to Connect MySQL Workbench to Running MySQL Inside Docker
How to Select All the Columns of a Table Except One Column
Convert Utc Milliseconds to Datetime in SQL Server
Oracle Sql: Extracting Text Between Two Characters
How to Add Leading Zero When Number Is Less Than 10
Multiplying Two Columns in SQL Server
Mysql - Operand Should Contain 1 Column(S)