how would you write a sql query to compute a frequency table of a certain attribute involving two joins? What changes would you need to make if you want to order by or group by some attribute? What would you do to account for nulls?
Answer / Satyendra Anuragi
To compute a frequency table with two joins, use SQL's JOIN and GROUP BY statements. Here is an example using the 'customers', 'orders', and 'order_items' tables where the target attribute is 'product_id'nn```sqlnSELECT order_items.product_id,n SUM(order_items.quantity) as total_quantitynFROM customersnJOIN orders ON customers.customer_id = orders.customer_idnJOIN order_items ON orders.order_id = order_items.order_idnGROUP BY order_items.product_id;n```nTo order by a certain attribute, use the ORDER BY statement at the end of your query like so:n```sqln... ORDER BY total_quantity DESC;n```nTo account for NULL values in your data, you can use the COALESCE function to replace NULLs with zeros or any default value.n```sqln... , COALESCE(order_items.quantity, 0) as quantity ...n```
| Is This Answer Correct ? | 0 Yes | 0 No |
How to generate create view script on an existing view?
I have to display ten columns values from diffrent ten tables. how many joins are require?
10 Answers CarrizalSoft Technologies, HCL,
Anyone please explain me the concept of Serialization?
What is difference between stored procedure and user defined function?
If no size is defined while creating the database, what size will the database have?
what is the difference between Delete and Truncate command in SQL
explain what is a schema in sql server 2005? Explain how to create a new schema in a database? : Sql server database administration
please give me query code of unique fuction select UNIQUE (name) from emp_info Incorrect syntax near the keyword 'UNIQUE'.
What is MSDE?
What happens if null values are involved in arithmetic operations?
What is the difference between UNIQUE and DISTINCT keywords in DBMS?
What is the difference between DTS and SSIS?
Oracle (3253)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)