Exclude a Column Using Select * [Except Columna] from Tablea

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



Leave a reply



Submit