As written:
CopySELECT CONCAT(area, yearlevel, code) AS SubjectCode, Count(student)
FROM StudentTakesSubject
WHERE result < 50 AND result <> NULL
GROUP BY code
HAVING Count(Student) > 1;
This query should return no rows. Why? result <> NULL returns NULL as a boolean value (to a close approximation all comparisons to NULL return NULL). AND NULL evaluates NULL -- and NULL is not true. All rows are filtered out.
The NULL comparison is actually superfluous. The result < 50 will also filter out NULL values.
Looking at the rest of the query, you have another issue. The GROUP BY is on code. It should really be on SubjectCode -- the result of the CONCAT(). In fact, when concatenating different columns, I would recommend using a separator, say CONCAT_WS(':', area, yearlevel, code). Of course a separator may not be desirable for this particular situation.
As written:
CopySELECT CONCAT(area, yearlevel, code) AS SubjectCode, Count(student)
FROM StudentTakesSubject
WHERE result < 50 AND result <> NULL
GROUP BY code
HAVING Count(Student) > 1;
This query should return no rows. Why? result <> NULL returns NULL as a boolean value (to a close approximation all comparisons to NULL return NULL). AND NULL evaluates NULL -- and NULL is not true. All rows are filtered out.
The NULL comparison is actually superfluous. The result < 50 will also filter out NULL values.
Looking at the rest of the query, you have another issue. The GROUP BY is on code. It should really be on SubjectCode -- the result of the CONCAT(). In fact, when concatenating different columns, I would recommend using a separator, say CONCAT_WS(':', area, yearlevel, code). Of course a separator may not be desirable for this particular situation.
result NOT NULL instead of result <> NULL.
CopySELECT CONCAT(area, yearlevel, code) AS SubjectCode, Count(student)
FROM StudentTakesSubject
WHERE result < 50
AND result NOT NULL
GROUP BY code
HAVING Count(Student) > 1;
I am working on a bunch of orders and I have an order date and the date that the order was "purchased". (The day we processed the order). If we never processed the order because the customer never followed through, there is a blank spot left in the purchased date resulting in a null when I run my query. I attempted to use IS NOT NULL to filter it out in the where statement but nothing happened. Any thoughts?
WHERE DATE(a.ORDER_DATE) BETWEEN DATE(:startDate) AND DATE(:endDate) AND a.PURCH_ORDER_DATE IS NOT NULL;Result of query
EDIT: For all of those who are wondering, I figured out how to sort out the null values.
SELECT DISTINCT
a.ORDER_NUMBER
,a.SALES_CATEGORY
,a.ORDER_DATE
,a.PURCH_ORDER_DATE
,CASE
WHEN DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE) >= 0 THEN DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE)
ELSE "null"
END as Lag
FROM
SOBOOK a
WHERE
DATE(a.ORDER_DATE) BETWEEN DATE(:startDate) AND DATE(:endDate)
AND
ceil(DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE)) = DATEDIFF(a.ORDER_DATE,a.PURCH_ORDER_DATE)
ORDER BY
ORDER_NUMBER
,Lagquery - Not equal to operator is not returning NULL values in SQL Server - Database Administrators Stack Exchange
ISNULL not working – SQLServerCentral Forums
"is not null" doesn't work | Access World Forums
How to see non null values?
Videos
Consider checking documentation:
NULL indicates that the value is unknown. A null value is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a null value and any other value, return unknown because the value of each NULL is unknown.
If you consider that the value of istrue is unknown in the NULL case then it might or might not equal 1.
The expression istrue != 1 then evaluates to unknown
SQL only returns rows where the WHERE clause evaluates to true.
If you are on SQL Server 2022+ you can use
WHERE istrue IS DISTINCT FROM 1
To give the inequality semantics that you want (Fiddle).
Hi! Me and my sibling-in-law are just beggining to learn SQL and are about to get in a boot camp that gives you an introductory "exam". We failed it the first time, but weren't told why. This Exam willl change, so we're not looking to have our homework done so to say, we just want to understand what we did wrong in the first try.
And after watching a lot of videos and trying different solutions, we're a bit confused about this schema:
What we can't get a grasp on is what's the use of NOT NULL here? Like, how should we add that to our querys?
We're also a bit lost when it comes to item 10, how should we use "join" here?
Thank you in advance, we're doing our best!
I'll translate all the questions so that there's some context:
The first point was:
"Write an SQL query to show all the products in the table "Productos" with a price higher to $50."
Our answer was:
Select * from productos where Price > 50
Second point was:
"Write an SQL query to obtain the total amount of orders (pedidos) made by an specific client according to his ID"
Our answer was:
Select cliente_ID, count(*) as Pedidos_count
from Pedidos
where cliente_ID= ‘NOT NULL’
group by cliente_ID
Third point was:
"Write an SQL query to update the price of a product on the table "Productos""
Our answer was:
Update productos set price = ‘Float’
where nombre = ‘Varchar’
Fourth point was:
"Write an SQL query to show the names of the products together with their corresponding categories."
Our answer was:
Select nombre_varchar, categoria_varchar from productos
Fifth point was:
"Write an SQL query to delete all the orders that have an amount lesser than 5."
Our answer was:
Delete from pedidos where quantity < 5
Sixth point was:
"Write an SQL query to calculate the total price of the orders made."
Our answer was:
Select SUM (total_precio) as "total_pedidos_precio"
From Pedidos
Seventh point was:
"Write an SQL query to show the names of the products in ascendant alphabetical order."
Our answer was:
select * from productos
Order by nombre asc
Eighth point was:
"Write an SQL query to show the orders made in a specific date." (fecha means date).
Our answer was:
select * from Pedidos where date (fecha_pedido) = NOT NULL
Ninth point was:
"Write an SQL query to obtain the average of the prices of all the products."
Our answer was:
Select AVG (precio) from Productos
Tenth point was:
"Write an SQL query to show the products together with the total amount of orders made for each one."
We weren't sure about this one, we think we have to use the join clause, but we couldn't agree on how to.
Eleventh point was:
"What's the correct syntax to insert a new record in the table "Usuarios" (Users)"
a) INSERT INTO Usuarios (Nombre, Apellido) VALUES ('John', 'Doe'); (Picked this one)
b) INSERT Usuarios (Nombre, Apellido) VALUES ('John', 'Doe');
c) INSERT VALUES ('John', 'Doe') INTO Usuarios;
d) INSERT INTO Usuarios VALUES ('John', 'Doe');
Twelfth point was:
"What's the function used to obtain the total amount of records in a table?"
a) COUNT() (Picked this one)
b) SUM()
c) AVG()
d) MAX()
Thirteenth point was:
"What's the clause used to filter results in a SELECT query?"
a) WHERE (Picked this one)
b) FROM
c) ORDER BY
d) GROUP BY
Fourteenth point was:
"What's the operator used to combine conditions in a WHERE clause?"
a) OR
b) AND (Picked this one)
c) NOT
d) XOR
Fifteenth point was:
"What's the SQL query to delete an existing table?"
a) DELETE TABLE name_table; (Picked this one)
b) DROP name_table;
c) REMOVE name_table;
d) ERASE name_table;
You dont have nulls but blank spaces add below in your query
SELECT project_id, monetary_amount,
journal_line_date
FROM PS_JRNL_LN
where ( project_id is not null or
( project_id is not
null
and LTRIM( RTrim(project_id)) not
like '')
and
journal_line_date BETWEEN
to_date ('2020/01/01','yyyy/mm/dd')
AND TO_DATE
('2020/03/04','yyyy/mm/dd')
Here is something that can help you find out what is happening in the project_id column. (Most likely, a bunch of ' ' values, meaning non-empty string consisting of a single space.)
select project_id, dump(project_id)
from ps_jrnl_ln
where ltrim(project_id, chr(32) || chr(9)) is null
and project_id is not null
;
DUMP shows you exactly what is stored in your table. 32 is the ASCII code for a single space; 9 (or 09) is the code for horizontal tab. I expect you will get rows where the DUMP column shows a single character, with code 32. But - who knows; you may find other things as well.
That will help you understand what's in the column. (You may also check describe ps_jrnl_ln - you may find out that the column is declared not null!!!)
If you find a bunch of rows where the project id is a single space, of course, in your actual query you will have to change
where project_id is not null
to
where ltrim(project_id, chr(32) || chr(9)) is not null
Or, perhaps, if indeed a single space is used as placeholder for null:
where project_id != ' '