SQL Queries for Student and Product Tables
SQL Queries for Student and Product Tables
SQL sorting (ORDER BY) and grouping (GROUP BY) enable detailed data structuring that underpins strategic business decisions. For instance, ranking products by sales volume or cost helps prioritize inventory, while grouping by attributes like color or size facilitates tailored marketing and logistics strategies, maximizing profit and efficiency .
The DISTINCT keyword is not typically used with GROUP BY because GROUP BY already aggregates unique groupings. However, you can use SELECT DISTINCT with GROUP BY for complex queries involving subqueries within the selection. In this dataset context, it's unnecessary as GROUP BY inherently isolates these distinct groupings .
The query: SELECT COLOUR, SUM(PRICE) FROM GARMENT GROUP BY COLOUR; calculates the total price value of garments per color. This aggregation helps in inventory management by providing insight into the financial value tied to each color, aiding decisions like restocking or sale planning .
The data will be displayed first sorted by SIZE in ascending order. For garments with the same SIZE, they will be further sorted by GNAME in descending order. The SQL query is: SELECT * FROM GARMENT ORDER BY SIZE ASC, GNAME DESC; .
To display student names in ascending order, use the query: SELECT * FROM STUDENT ORDER BY NAME; For descending order, use: SELECT * FROM STUDENT ORDER BY NAME DESC;
Use the query: SELECT PRICE, SUM(CALORIES) FROM SOFTDRINK GROUP BY PRICE HAVING SUM(CALORIES) > 30; This combines SUM and HAVING to filter out price groups with a calorie total exceeding 30, useful for identifying potentially less profitable price segments .
Using GROUP BY SIZE with HAVING COUNT(*) > 1 (SELECT SIZE, COUNT(*) FROM GARMENT GROUP BY SIZE HAVING COUNT(*) > 1;) identifies sizes that might require additional inventory review. This can indicate popular or overstocked sizes, guiding restocking or selling strategies .
Use the query: SELECT CITY, COUNT(*) FROM STUDENT GROUP BY CITY HAVING COUNT(*) > 1; The HAVING clause filters groups based on a condition. Here, it ensures that only cities with more than one student are included .
Using multiple columns like SELECT * FROM PRODUCT ORDER BY QUANTITY ASC, PRICE DESC; first sorts results by QUANTITY ascending and then by PRICE descending for equal quantities. This provides hierarchical data view, enhancing analysis by showing tiered prioritization .
Use the query: SELECT DRINCODE, DNAME, CALORIES FROM SOFTDRINK ORDER BY CALORIES DESC; This ordering is useful for prioritizing high-calorie drinks, allowing nutritionists to quickly identify products that may need to be re-evaluated for health concerns .