Total members 9936 | Gratitudes |It is currently Sun Feb 05, 2012 8:18 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Step-3- in SQl-server2000
PostPosted: Sat Apr 07, 2007 7:12 pm 
Offline
Newbie
User avatar

Joined: Thu Apr 05, 2007 9:57 pm
Posts: 17
Has thanked: 0 time
Have thanks: 0 time

DML Statements(Advanced)

Code:
SELECT <column list>
FROM <table names>
[WHERE <condition>]
[GROUP BY <column list>]
[HAVING <condition>]
[ORDER BY <column list>]



1. Using Group by and Order by

Example 1: (without aggregate function equivalent to using distinct)

Give all different unit prices from the product table ordered Ascendingly

Code:
SELECT unitprice
FROM products
GROUP BY unitprice
ORDER BY unitprice Asc;                 


Example 2:

For each unit price, find the number of products having that unit price ordered Descendingly by unit price with a not null unit price

Code:
SELECT unitprice, count(*)
FROM products
WHERE unitprice <> NULL
GROUP BY unitprice
ORDER BY unitprice Desc;   


Count is a Group Function , search for others and see example(s) on them? ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط·آ¥أ¢â‚¬إ“Grouping columnsط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â€ڑآ¬ط¥â€™

2.Using Having Clause

Example 1:

What are the cities where more than 1 employee live?

Code:
SELECT city
FROM employees
GROUP BY city
HAVING count (employeeid) >1;


Example 2:

Get the id of the customer that ordered more than one order

Code:
SELECT customerid , count (*) [as Orders]
FROM orders
GROUP BY customerid
HAVING count (*) >1;



3.Joins

ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط·آ¥أ¢â‚¬إ“How about making example that get you name of customers ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط·آ¹ط¢آ©CompanyNameط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â‚¬â€چط¢آ¢ who have more than one order?ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â€ڑآ¬ط¥â€™

Code:
Select Customers.companyName as Name, count(Orders.Customerid) as Orders
From Orders , Customers
where Orders.Customerid = Customers.Customerid
group by (Customers.companyName);


Example 1:

Get the names, prices and supplier names for each product

Code:
SELECT  Products.ProductName,Suppliers.ContactName,Products.UnitPrice
FROM Products, Suppliers
WHERE Products.SupplierID = Suppliers.SupplierID


Example 2:

State the supplier names for each category

Code:
SELECT Categories.CategoryName, Suppliers.CompanyName
FROM Categories,Products, Suppliers
WHERE  Suppliers.SupplierID = Products.SupplierID 
        AND Products.CategoryID = Categories.CategoryID;


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 unit step and impulse function L.T  General Discussion  loomy  0
 (MS Press) Microsoft Visual CSharp 2008 - Step By Step  offers  Casper  0
 Cisco - Press - Configuring CallManager and Unity A Step-by-  offers  Casper  0
 TCP IP First Step  offers  Casper  0
 Cisco Press - Routing First-Step 2004  offers  Casper  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team