Topic 08 : SQL - 2

1,480 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,480
On SlideShare
0
From Embeds
0
Number of Embeds
824
Actions
Shares
0
Downloads
97
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Topic 08 : SQL - 2

  1. 1. SQL - 2 Er. Pradip Kharbuja
  2. 2. What is NULL?  NULL means Unknown or nonexistent.  NULL is not a value.  It is not zero or blank or an “empty string”.  NULL is a special marker used in SQL to indicate that a data value does not exist in the database.  If A and B are NULL, what is the result of A = B?
  3. 3. Data Definition CREATE TABLE departments ( dept_no INTEGER NOT NULL, department_name VARCHAR(30), location VARCHAR(30) PRIMARY KEY (dept_no) );
  4. 4. Primary Key  Unique Value + Not NULL = Primary Key  A primary key cannot allow NULL values.  Each table can have at most one primary key.
  5. 5. Modifying Tables Using SQL  Add an extra column  Drop a column from a table  Modify the maximum length of the table  Add a new constraint like Primary Key, Unique Key  Drop a constraint  Set a default value for a column  Drop a default value for a column
  6. 6. Adding & Removing new column  ALTER TABLE departments ADD department_head VARCHAR(30);  ALTER TABLE departments ALTER COLUMN department_head VARCHAR(50);  ALTER TABLE departments DROP COLUMN department_head;
  7. 7. Data Retrieving  Select  Order By  Aggregate functions  Group by  Subqueries  Joins
  8. 8. SELECT  SELECT * FROM departments;  SELECT * FROM departments WHERE dept_no = 1;  SELECT dept_no, department_name FROM departments;  SELECT department_name FROM departments WHERE dept_no = 2;
  9. 9. ORDER BY  to get result in ascending or descending order.  SELECT * FROM departments ORDER BY dept_no;  SELECT * FROM departments ORDER BY dept_no ASC;  SELECT * FROM departments ORDER BY dept_no DESC;  SELECT department_name, location FROM departments WHERE dept_no = 1 ORDER BY department_name DESC;
  10. 10. workers Table Column Name emp_no first_name last_name job_title age dept_no Type Integer Varchar Varchar varchar Integer Integer Length 30 30 30 NULL Not Null Key Primary Key
  11. 11. workers Table emp_no 1 2 3 4 5 6 7 first_name Lawrence Jason Emily Ahmed Joe Hattie Sally last_name Surani Argo Villa Mukani Todj Smith Boorman job_title Manager Manager Manager Packer Packer Accountant Admin Assistant age 56 33 32 23 24 56 34 dept_no 1 2 3 1 1 2 3
  12. 12. Aggregation Functions 1. Count – returns number of values in a column 2. Sum – returns the sum total of values of a column 3. Avg – returns the mean average of values in column 4. Min – returns the lowest value in a column 5. Max – returns the highest value in a column
  13. 13. Aggregation Functions  SELECT COUNT(emp_no) FROM workers;  SELECT COUNT(emp_no) AS number_of_workers FROM workers;  SELECT SUM(age) FROM workers;  SELECT AVG(age) FROM workers;  SELECT MAX(age) FROM workers;  SELECT MIN(age) FROM workers;
  14. 14. GROUP BY  Display dept_no and count the number of workers in that department.  SELECT dept_no, COUNT(emp_no) FROM workers;  This query will produce an error.  SELECT dept_no, COUNT(emp_no) FROM workers GROUP BY dept_no;
  15. 15. GROUP BY  SELECT dept_no, COUNT(dept_no) FROM workers WHERE dept_no >=2 GROUP BY dept_no;  SELECT dept_no, COUNT(dept_no) FROM workers WHERE dept_no >=2 GROUP BY dept_no ORDER BY dept_no DESC;  Display dept_no which has more than 2 workers.  SELECT dept_no FROM workers GROUP BY dept_no HAVING COUNT(emp_no) > 2;
  16. 16. Sub Queries  A Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE clause.  Subqueries must be enclosed within parentheses.  A subquery can have only one column in the SELECT clause  Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator
  17. 17. Sub Queries  Display the department_name of the depeartment having the highest dept_no.  SELECT department_name FROM departments WHERE dept_no = (SELECT MAX(dept_no) FROM departments);
  18. 18. Joins  An SQL JOIN clause is used to combine rows from two or more tables, based on condition.  Types of Join 1. 2. 3. 4. 5. Cross Join or Cartesian Product Inner Join Left Join Right Join Full Join
  19. 19. Cartesian Join or Cross Join  Concept is like Cartesian Product.  eg. A = {1, 2, 3}, B = {a, b}  Cartesian Product will be : A x B = {(1, a), (1, b), (2, a), (2, b), (3, a), (3, b)}
  20. 20. Cartesian Join or Cross Join  SELECT * FROM departments, workers;
  21. 21. Inner Join  SELECT * FROM departments, workers WHERE departments.dept_no = workers.dept_no;
  22. 22. Key clauses in an SQL SELECT statement  SELECT – specifies which columns from the table are to appear in the result  FROM – specifies which table or tables are to be used to get the results  WHERE – specifies some condition that will restrict the rows that are retrieved  GROUP BY – groups rows by some column value  HAVING – used to restrict the result that will be grouped  ORDER BY – specifies the order in which the result will appear
  23. 23. Query Optimisation  Making sure a query runs as efficiently and as quickly as possible  The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plans.  eg. TOAD  Available at http://www.quest.com/toad/
  24. 24. ANY QUESTIONS? SQL - 2
  25. 25. Slides available @ http://www.pradipkharbuja.com.np/softwarica/database

×