SQL based Northwind, hard time on filtering

northwind database query exercises with answers
query execution steps in sql server
sql server query processing architecture
sql server query optimizer
sql server query optimizer tool
sql server architecture microsoft docs
sql server architecture pdf
wildcards at the end of search patterns take longer to run

So in a practice site there is a question:

Which Product is the most popular? (number of items)

This means that There are Customers, and they want to know the most popular Ordered Product by the Customers(Overall Orders of TOP 1 ordered Product). I Sincerely do not know How to solve this one. Any help? What I've tried so far is:

SELECT TOP(1) ProductID, ProductName
FROM Products
GROUP BY ProductID, ProductName
ORDER BY COUNT(*) DESC

But that's far from what they have asked. In this one, I just get the top 1 Product with the lowest count, but that doesn't mean anything about the customers who ordered this product. That only means that this specific Item could have been at low quantity and still is lower then the others, while the others were very high quantity and now just low (but still not low enough) I hope I was clear enough.

If the data exists in that table, you might just need to order by something more sophisticated than count, like summing the quantity (if that column exists). Also, if ProductID and ProductName are already unique identifiers, note that you don't need the group by and sum at all.

SELECT TOP(1) ProductID, ProductName FROM Products GROUP BY ProductID, ProductName ORDER BY SUM(Quantity) DESC

I don't know what your keys are, but it sounds like you actually want to be counting how many times it was ordered by customers, so you may need to join on the Customers table. I am assuming here that you have a table Orders, that has one line per order and shares the ProductID key. I also assume that ProductID is unique in Products (which may not be true based on your first query).

SELECT TOP(1) Products.ProductID, Products.ProductName FROM Products LEFT JOIN Orders ON Orders.ProductID = Products.ProductID GROUP BY Products.ProductID, Products.ProductName ORDER BY COUNT(Orders.OrderID) DESC

This really depends on what tables and keys you have available to you.

Pro LINQ in VB8: Language Integrated Query in VB 2008, Also notice that there is a single SQL statement that was performed. You can see that both of the filter criteria, the city and country, do appear in the Where Programmatically Building Another Query Dim db As New Northwind ("Data Source=. Out ' Pretend the values below are not hard-coded, but instead, obtained by  Many of Code On Time tutorials are using Northwind sample database created by Microsoft to illustrate concepts of database design and implementation with Microsoft SQL Server. The database is somewhat outdated and does not reflect the latest advancements in Microsoft SQL Server features and capabilities.

Select top 1 P.ProductID,P.ProductName,Sum(OD.Quantity)AS Quantity
 From [Order Details] OD
       inner join Products P ON P.ProductID = OD.ProductID
       Group By P.ProductID,P.ProductName
       Order by Quantity Desc

Query Processing Architecture Guide, The methods used to compute calculations, and how to filter, aggregate, Transact-SQL statements, the relational engine may have difficulty  SQL Views in MySQL Northwind database SQL view is a virtual dataset which does not contain any data. You can use view just like a normal table in queries, but the result of the view actually comes from a base table or another view.

You can workout something like this, (Table name/schema may differ)

with cte_product
as
(
select ProductID,Rank() over (order by Count(1) desc) as Rank from
Orders O
inner join Product P
on P.ProductID = O.ProductID
group by ProductID
)
select P.productID, P.ProductName from
cte_product ct
inner join product p
on ct.productId = p.ProductID
where ct.Rank = 1

Crux is usage of RANK() to get most popular product. Rest you may fetch columns as per need using relevant Joins.

Filtering data with SQL Server, The Northwind database is one of the two sample databases included with a SQL Server installation. It's not perfect, but it's fine for the purpose of  Click OK button to finish restore your database to SQL Server. What is the Northwind database? The Northwind database is a sample database provided with Microsoft Office suite. The Northwind database contains sales data of a fictional company called Northwind Trader, specializing in the export and import of specialty foods from around the world.

Network World, "But they are hard to deploy and use, and they result in a huge amount of One of the first things Mike Todd, a principal with Northwind Consulting in PcAnywhere provides remote control connectivity that mirrors an NT server console Shope says at the time Novell, Inc. had not addressed NT/NetWare integration issues. This is part 2 of the tutorial series - converting the popular Microsoft Access Northwind database queries to MySQL queries. These queries are originated from Access Northwind Traders application. Some of them are relatively complex aggregated queries with sub-queries.

Using Wildcards in SQL, Video created by University of California, Davis for the course "SQL for Data Science". a median base salary of $110,000 and thousands of job openings at a time. In the Northwind database, some of the product descriptions will tell you how great way to start to parse out that information that may be hard to retrieve. The popular Microsoft Access Northwind database has been exported to MySQL here. This is part 1 of this tutorial series - we convert the Northwind Access queries to MySQL SQL queries. These queries are used for Access Northwind Traders application. Some of them are relatively complex aggregated queries.

MySQL Northwind database, Products table, Exercises, practice with solution on Northwind Products table, MySQL select statements; exercises on order by, functions, calculation, and more  northwind-SQLite3. This is a version of the Microsoft Access 2000 Northwind sample database, re-engineered for SQLite3. The Northwind sample database was provided with Microsoft Access as a tutorial schema for managing small business customers, orders, inventory, purchasing, suppliers, shipping, and employees.

Comments
  • Is there an Orders table?
  • Yes. Indeed @Mihn Northwind is a familiar database of Microsoft for practicing purposes
  • Think about popularity. Most popular product can be a) that gave most sale amount, b) sold in greatest number, and c) sold to greatest number of unique customers. Old good Northwind keep all this information. You have to learn how to ask him. Hint: Look at Orders and Order Details tables.
  • Welcome to StackOverflow.While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • Totally wrong answer. Orders table doesn't have productID field. Northwind is still available for download from Microsoft, and @Akanksha Singh should have done it first.
  • With due respect Sir @Alex, Thanks for the suggestion but I have full fledged databases of my company to work upon. And I don't find it a pre-requisite to memorize your favourite NORTHWIND for being a SQL developer. I agree that I no more have NORTHWIND which I had long back in my basic learning phase. Moreover, if you read my answer again,as the user N3wbie, like you, is also a big fan of Northwind, and didn't share the schema, that is why I mentioned that he can 'workout something like this' and suggested to go for RANK() and relevant joins.
  • Dear @Akanksha, the question came from a freshman who can hardly appreciate CTE and RANK functions. They should learn simple things first and don't blow their brains with very advanced things. With all respect...