Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 25 days ago.
Improve this question
I am starting to learn SQL and I had a question.
I am running the following query:
SELECT ProductName
FROM Products
WHERE ProductID =ALL (
SELECT ProductID
FROM OrderDetails
WHERE Quantity < 0
)
order by ProductID;
Why does this query return values if I am looking for quantity less than zero?
In the OrderDetails table, there are no quantity less than zero. So shouldn't the output of this query be empty?
Is there something I am missing.
w3schools SQL Practice
If your subquery has a WHERE clause that is always false, then the subquery returns an empty result.
https://dev.mysql.com/doc/refman/8.0/en/all-subqueries.html says:
Finally, the expression is TRUE if table t2 is empty. So, the
following expression is TRUE when table t2 is empty:
SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
Demo: https://dbfiddle.uk/zCO71Q15
An ALL predicate means it's false if at least one row of the subquery causes the comparison to be false.
But if the subquery returns zero rows, then of course there can't be any rows that cause the predicate to be false.
Is it any clearer written as
SELECT p.*
FROM Products p
WHERE EXISTS (
SELECT NULL
FROM OrderDetails o
WHERE o.ProductID = p.ProductId
AND o.Quantity = 10
);
I.e., find all products where an order has been placed for exactly 10 of them.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this query with returned rows a alias and in keyword in mysql.I would like to delete all returned row that value is less or equal to 0 in the same table.
delete from tblmusterproject where final in(
SELECT final from (select sum(Crowd+Review+Lp+additions+deductions+monthly) as final,
employeeno FROM tblmusterproject
group by employeeno) as b) <=0;
am get this Error "Unknown column 'final' in 'IN/ALL/ANY subquery'"
How can i do it.
You have <=0 that seems in wrong position and yout
If you want delete the employeeno when filter value (alias the sum) <= 0 then you could use having in subquery for get the employeeid you need
and instead of a in clause you could use a join
delete t
from tblmusterproject t
INNER JOIN (
Select sum(Crowd+Review+Lp+additions+deductions+monthly) as final, employeeno
FROM tblmusterproject
group by employeeno
HAVING final = <=0
) b on b.employeeno = t.employeeno
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i have two table alamat_penerima and lokasi
alamat penerima table
[this is my record
and then
lokasi table [this is the record
i wont to make two table like this
[view]
I hope you can help me
i have just try this query command :
SELECT COUNT(alamat_penerima.`nama_penerima`) AS penerima, lokasi.`wilayah`
FROM lokasi
INNER JOIN alamat_penerima ON alamat_penerima.`kota_kab` = lokasi.`wilayah`
GROUP BY alamat_penerima.`kota_kab`
but the result is
result
Your question is difficult to understand, so this answer is guesswork. It looks to me like you hope to list the number of recipient addresses in each region, but also show the regions with no addresses.
To do that, you need two subqueries joined together.
This subquery generates the names of all regions.
SELECT DISTINCT wilayah FROM lokasi
This subquery generates a list of regions with the count of addresses in each region. The result set from this subquery, however, omits regions with no addresses in them.
SELECT COUNT(*) num, kota_kab AS wilayah
FROM alamat_penerima
GROUP BY kota_kab
You can test these subqueries individually to determine whether they are correct. It's important to do that.
Finally, you join these two together as if they were tables. (That's why it's called structured query language).
SELECT w.wilayah, a.num
FROM (
SELECT DISTINCT wilayah FROM lokasi
) w
LEFT JOIN (
SELECT COUNT(*) num, kota_kab AS wilayah
FROM alamat_penerima
GROUP BY kota_kab
) a ON w.wilaya = a.wilaya
This will yield what you want, but showing NULL instead of 0 in rows with no addresses. (That's a result of using LEFT JOIN) You can put the 0 values there by using this as the first line of your query instead.
SELECT w.wilayah, IFNULL(a.num,0) num
The design trick is to make your first subquery determine the number of rows in your result set, then to use LEFT JOIN to put information from subsequent subqueries into your results.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
SELECT sum(st.stock_in) - sum(st.stock_out) as'stock_qty',
p.product_name,p.product_limit
FROM stock as st
join products as p
on p.product_id=st.product_id
where 'stock_qty' < 'p.product_limit'
GROUP BY st.product_id
I Have two Tables Products and Stock.
Products Table: Product_id, Product_name and Product_limit
Stock: stock_id, Product_id, stock_in, stock_out
From above query i get all the records, but I want to get only those records who product limit is reached or below.
if product 1 has 50 product_limit and stock reached the 50 limit or below the 50 so get me those records.
Suggest me best solution.
You cannot:
Use an alias in a where clause.
Use aggregation functions in the where clause.
Although comparing two strings is pretty much not ever recommended, it is allowed.
But, you want a having clause anyway, so the question in your title is irrelevant. Try this:
select sum(st.stock_in) - sum(st.stock_out) as stock_qty,
p.product_name, p.product_limit
from stock st join
products p
on p.product_id = st.product_id
group by p.product_name, p.product_limit
having stock_qty < p.product_limit ;
Note that I changed the group by to match the columns being selected in the select. That is also a good practice.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two tables
Table A : Content Question , its options and correct answer
Table B : This table is for time allow to answer the question in given time.
this table has Question_Id field which either have question id or zero. zero means if for Table A Question Id is not found in Table B then default Time will be Table B's Question_Id=0 > 5 Min
Now I want the data like Result table from query. By using Select Query with Join I am getting question details, which are matched with question Id (1,2,4) means for Question 3,5,6 row not getting that showing in result table.
Please suggest what sql query should write so that I can get result like Result Table's content.
I change your tables to small and simple tables and you can see the result in:
SQL Fiddle
or try this query:
SELECT t1.questionid,
t1.question,
t1.options,
t1.answer,
COALESCE(t2.timingstatement, '5 Min') TimingStatement
FROM tablea t1
LEFT OUTER JOIN tableb t2
ON t1.questionid = t2.questionid;
Try this
SELECT Q.QuestionID,Q.Question,Q.Options,QAnswer,
CASE WHEN Q.QuestionID NOT IN (SELECT QuestionID FROM Table2) THEN '5 Min'
ELSE T.TimingStatement
END [TimingStatement]
FROM Table1 Q
JOIN Table2 T ON Q.QuestionID = T.QuestionID