SQL Error unknown column in where clause? [duplicate] - sql

This question already has answers here:
How do I use alias in where clause? [duplicate]
(4 answers)
Closed 2 years ago.
Write a query to display hotel id, hotel name, and number of orders taken by hotels that have taken orders more than 5 times. Give an alias name for number of orders as 'NO_OF_ORDERS'.sort the result based on hotel id in ascending order.
Data Base Image
The Query I wrote:
select hotel_details.hotel_id, hotel_name, count(orders.hotel_id) as no_of_orders
from hotel_details join orders on hotel_details.hotel_id = orders.hotel_id
where no_of_orders > 5
group by orders.hotel_id order by no_of_orders;
Error I go:
unkown column 'number_of_orders' in 'where clause'

You need a HAVING clause, not a WHERE clause:
select
hd.hotel_id,
hd.hotel_name,
count(o.hotel_id) as no_of_orders
from hotel_details hd
inner join orders o on hd.hotel_id = o.hotel_id
group by
o.hotel_id
having
no_of_orders > 5
order by
no_of_orders;
The count of orders occurs during the GROUP BY portion of the query, at which point WHERE has already happened. So, assertions on anything from GROUP BY belong in a HAVING clause.

Related

Students with Highest Mark [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 1 year ago.
Write a query to display the student names and the maximum mark scored by them in any subject, ordered by name in ascending order. Give an alias to the maximum mark as MAX_MARK.
I am not able to find the logic for this. Kindly help me with it. Do it in oracle SQL I am at beginner level in SQL.
SELECT MAX(M.VALUE), S2.SUBJECT_ID,M.STUDENT_ID, S2.SUBJECT_NAME,S2.SUBJECT_CODE
from Mark M INNER JOIN SUBJECT S2
ON M.SUBJECT_ID=S2.SUBJECT_ID group BY S2.SUBJECT_ID,
S2.SUBJECT_CODE, S2.SUBJECT_NAME;
I am getting error with this query if I get this student id with the help of the above query then I can easily solve this question using subquery concept.
You don't need subject there. Question asks Max mark per student, regardless of subject:
SELECT s.Student_Name, MAX(M.VALUE) as MAX_MARK
from Student s
inner Join Mark M on m.student_id = s.student_id
group by s.student_id, s.student_name
order by s.student_name;

Sum of data at each row in different column in sql [duplicate]

This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 4 years ago.
I need to create a different column of total sales which is the total of the sales units till that row. As our sales unit will increase, the total sales will increase row by row. I have attached the image to get the clear idea.
Screenshot
One method is use to use of subquery with correlation approach
select *,
(select sum(sales) from table where product = t.product and ? <= t.?) TotalSales
from table t
However, you would required ? (i.e. rowid or id) column that could specify your column ordering.
You can use the following query, which adds up all the sales upto that rowId and for the specific product.
select (select sum(ids)
from TBL_NAME T1
where T1.rowid <= TBL_NAME.rowid and PRODUCT=TBL_NAME.PRODUCT
) as TotalSales
from TBL_NAME;
Following answer is for Oracle DB, you can understand the query and easily convert it to the product you want.

Very basic SQL script with aggregate [duplicate]

This question already has answers here:
"You tried to execute a query that does not include the specified aggregate function"
(3 answers)
Closed 6 years ago.
select Customers.cust_id, count(Orders.cust_id)
from Customers left outer join Orders
on Customers.cust_id=Orders.cust_id
group by Customers.cust_id
This correctly displays everything.
select Customers.cust_id, ***Customers.cust_name***, count(Orders.cust_id)
from Customers left outer join Orders
on Customers.cust_id=Orders.cust_id
group by Customers.cust_id
,,Your query does not include the specified expression 'cust_name' as port of an aggregate function."
Why is that? Each cust_id in Customers has a name in cust_name. Why do I get this error message?
When you use an aggregate function count() all other fields (that aren't used with an aggregate function) must appear in the Group By clause.
Here is my explanation as to why:
Aggregate functions operate across groups.
(That is, unless no groups or other fields are specified, in which case they operate across the whole recordset by default. For example, SELECT Sum(Salary) FROM Staff works.)
If you group by cust_id then it knows what to output, a count for each cust_id. But what would it do with the cust_name's? Which cust_name would it, or should it, display for each cust_id output? What if there are several cust_name's for a cust_id? It will only display one row for each cust_id, so what name should it display alongside it? It won't make the assumption that there is exactly one cust_name to correspond to one cust_id.
If there is one cust_name per cust_id then grouping by both will produce the same number of rows (as for cust_id alone) and provide consistent, and reliable, behaviour.
select Customers.cust_id, Customers.cust_name, count(Orders.cust_id)
from Customers left outer join Orders
on Customers.cust_id=Orders.cust_id
group by Customers.cust_id, Customers.cust_name

SQL Error: ORA-00979: not a GROUP BY expression [duplicate]

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 8 years ago.
Can anyone please help me figure this out,
I have 3 tables: Customer, Products and Products_ordered and I am trying to find customers who have ordered more than 1 product. Here is my query:
SELECT customer_id, product_id
FROM product_ordered
GROUP BY customer_id
HAVING COUNT (customer_id)>1;
I am getting this error:
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
Thanks for helping
try
select customer_id, product_id from product_ordered group by customer_id,product_id having count (customer_id)>1;
Try:
SELECT customer_id
FROM product_ordered
GROUP BY customer_id
HAVING COUNT (customer_id)>1;
The issue is that product_id is not part of the group by. If you do a group by, you can only select columns in the group by or use an aggregate function. That query will return the customer_id's that occur more then once. I don't know your table structure, but if you want more data then just the id let us know what sql version you are using, SQL Sever, MYSQL, or Oracle, and I can try to write something with windowing functions.
Don't you really want to select Customers who ordered more than one product?
More than one order line, or more than one product, or more than one unique product?
If you run as an inline query
(select customer_id from product_ordered group by customer_id having count (customer_id) > 1)
You will see all customers who placed more than one order line. But there could be multiple lines in an order, or multiple orders of one line, yada yada...
Try select customer_id from product_ordered group by customer_id having count(distinct product_id)>1 which will let you actually see customers who bought more than one unique product.

How to find doubles with SQL? [duplicate]

This question already has answers here:
Select statement to find duplicates on certain fields
(9 answers)
Closed 8 years ago.
Using the Chinook Test Database I wrote this SQL statement to show all the tracks ordered by two specific customers:
SELECT inv.BillingCity,cus.LastName,tra.Name
FROM invoice AS inv
JOIN customer AS cus ON inv.CustomerId=cus.CustomerId
JOIN invoiceline inl ON inv.InvoiceId=inl.InvoiceId
JOIN track tra ON tra.TrackId=inl.TrackId
WHERE cus.LastName IN ('Schneider','Schröder')
ORDER BY inv.BillingCity,cus.LastName,tra.Name
I see that there is a track that was ordered twice by one customer:
How would I write an SQL statement to find doubles like this, i.e. "return all tracks ordered multiple times by one customer"?
Try this:
SELECT cus.CustomerId,tra.Name,COUNT(cus.CustomerId) AS tot
FROM invoice AS inv
JOIN customer AS cus ON inv.CustomerId=cus.CustomerId
JOIN invoiceline inl ON inv.InvoiceId=inl.InvoiceId
JOIN track tra ON tra.TrackId=inl.TrackId
GROUP BY cus.CustomerId,tra.Name
HAVING tot > 1