It is currently Fri Jul 30, 2010 2:07 pm


All times are UTC [ DST ]


Ask on Codemiles community and get answers Free and Fast :

SQLServer codes,SQLServer tricks ,SQLServer faq ,SQLServer answers .and more

Our guest share with us your code snippets , your programming problems , your open source projects ,read articles and post yours .







Post new topic Reply to topic  [ 1 post ] 
  Print view Previous topic | Next topic 
Author Message
 Post 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  
 
| More
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


 Similar topics
 Topic title   Forum   Author   Replies 
 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 ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  





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-2009
mileX v1.0 designed by codemiles team