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?



how would you write a sql query to compute a frequency table of a certain attribute involving two jo..

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

Post New Answer

More SQL Server Interview Questions

How to generate create view script on an existing view?

1 Answers  


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?

3 Answers  


What is difference between stored procedure and user defined function?

1 Answers  


If no size is defined while creating the database, what size will the database have?

1 Answers  


what is the difference between Delete and Truncate command in SQL

1 Answers   BirlaSoft,


explain what is a schema in sql server 2005? Explain how to create a new schema in a database? : Sql server database administration

1 Answers  


please give me query code of unique fuction select UNIQUE (name) from emp_info Incorrect syntax near the keyword 'UNIQUE'.

1 Answers   TCS,


What is MSDE?

2 Answers  


What happens if null values are involved in arithmetic operations?

1 Answers  


What is the difference between UNIQUE and DISTINCT keywords in DBMS?

1 Answers   Genpact,


What is the difference between DTS and SSIS?

3 Answers   Allianz,


Categories