06.02 sql alias

302 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
302
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

06.02 sql alias

  1. 1. S Q LDBMSBishal Ghimire
  2. 2. SQL Alias With SQL, an alias name can be given to a table or toa column Syntax SELECT column_name(s)FROM table_nameAS alias_name SELECT O.OrderID, p.LastName, p.FirstNameFROM Persons AS p,Product_Orders AS OWHERE p.LastName=Hansen AND p.FirstName=Ola
  3. 3. SELECT statement without aliases SELECT Product_Orders.OrderID, Persons.LastName,Persons.FirstNameFROM Persons,Product_OrdersWHERE Persons.LastName=Hansen ANDPersons.FirstName=Ola
  4. 4. SQL AVG() Function The AVG() function returns the average value of anumeric column Syntax SELECT AVG(column_name) FROM table_name Eg- SELECT AVG(OrderPrice) AS OrderAverage FROMOrdersTable OutputOrderAverage950
  5. 5. SQL AVG() Function To find the customers that have an OrderPrice valuehigher than the average OrderPrice valueSELECT CustomerFROM OrdersWHERE OrderPrice >(SELECT AVG(OrderPrice) FROM Orders)
  6. 6. COUNT() Function Syntax SELECT COUNT(column_name) FROM table_name SELECT COUNT(*) FROM table_name SELECT COUNT(DISTINCT column_name) FROMtable_name Eg SELECT COUNT(Customer) AS CustomerNilsen FROMOrdersWHERE Customer=Nilsen
  7. 7. MAX() Function The MAX() function returns the largest value of theselected column. Syntax SELECT MAX(column_name) FROM table_name Eg SELECT MAX(OrderPrice) AS LargestOrderPrice FROMOrders
  8. 8. MIN() Function MIN() function returns the smallest value of theselected column Syntax SELECT MIN(column_name) FROM table_name EgSELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
  9. 9. HAVING Clause The HAVING clause was added to SQL because theWHERE keyword could not be used with aggregatefunctions Syntax SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value
  10. 10. HAVING Clause SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY CustomerHAVING SUM(OrderPrice)<2000 SELECT Customer,SUM(OrderPrice) FROM OrdersWHERE Customer=Hansen OR Customer=JensenGROUP BY CustomerHAVING SUM(OrderPrice)>1500
  11. 11. O_Id OrderDate OrderPrice Customer1 2008/11/12 1000 Hansen2 2008/10/23 1600 Nilsen3 2008/09/02 700 Hansen4 2008/09/03 300 Hansen5 2008/08/30 2000 Jensen6 2008/10/04 100 NilsenCustomer SUM(OrderPrice)Nilsen 1700SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY CustomerHAVING SUM(OrderPrice)<2000
  12. 12. HAVING Clause SELECT Customer,SUM(OrderPrice) FROM OrdersWHERE Customer=Hansen OR Customer=JensenGROUP BY CustomerHAVING SUM(OrderPrice)>1500O_Id OrderDate OrderPrice Customer1 2008/11/12 1000 Hansen2 2008/10/23 1600 Nilsen3 2008/09/02 700 Hansen4 2008/09/03 300 Hansen5 2008/08/30 2000 Jensen6 2008/10/04 100 NilsenSELECT Customer,SUM(OrderPrice) FROM OrdersWHERE Customer=Hansen OR Customer=JensenGROUP BY CustomerHAVING SUM(OrderPrice)>1500Customer SUM(OrderPrice)Hansen 2000Jensen 2000
  13. 13. GROUP BY Statement The GROUP BY statement is used in conjunctionwith the aggregate functions to group the result-setby one or more columns Syntax SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name
  14. 14. O_Id OrderDate OrderPrice Customer1 2008/11/12 1000 Hansen2 2008/10/23 1600 Nilsen3 2008/09/02 700 Hansen4 2008/09/03 300 Hansen5 2008/08/30 2000 Jensen6 2008/10/04 100 NilsenTo find the total sum (total order) of each customer.We will have to use the GROUP BY statement to group the customers.
  15. 15. O_Id OrderDate OrderPrice Customer1 2008/11/12 1000 Hansen2 2008/10/23 1600 Nilsen3 2008/09/02 700 Hansen4 2008/09/03 300 Hansen5 2008/08/30 2000 Jensen6 2008/10/04 100 NilsenSELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY CustomerCustomer SUM(OrderPrice)Hansen 2000Nilsen 1700Jensen 2000
  16. 16.  what happens if we omit the GROUP BY statementCustomer SUM(OrderPrice)Hansen 5700Nilsen 5700Hansen 5700Hansen 5700Jensen 5700Nilsen 5700

×