• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
BIS05 Introduction to SQL
 

BIS05 Introduction to SQL

on

  • 2,681 views

Course Material for MBA Course on Business Information Systems

Course Material for MBA Course on Business Information Systems

Statistics

Views

Total Views
2,681
Views on SlideShare
2,608
Embed Views
73

Actions

Likes
1
Downloads
211
Comments
0

2 Embeds 73

http://yantrajaal.ning.com 72
http://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    BIS05 Introduction to SQL BIS05 Introduction to SQL Presentation Transcript

    • Business Information Systems Introduction to SQL Prithwis Mukerjee
    • Categories of SQL
      • Data Retrieval
        • Select
      • Data Manipulation Language (DML)
        • Insert
        • Delete
        • Update
      • Transaction Control
        • Commit
        • Rollback
      • Data Definition Language (DDL)
        • Create
        • Alter
        • Drop
      • Data Control
        • Grant
        • Revoke
    • Basics of Retrieval - 1 : Projection & Selection
      • Projection …. [ a subset of COLUMNS ]
        • Select Emp_ID, LastName, Salary from Employee
        • Select * from Employee
      • Selection … [ a subset of ROWS ]
        • Select * from Employee where Dept = ’40’
    • Basics of Retrieval – 2 : JOIN
      • Select EmpID, LastName, FirstName, Location from Employee, Department where Employee.Dept = Department.Dept_ID
      JOIN
    • The Retrieval Process Select EmpID, LastName, FirstName, Location from Employee, Department where Employee.Dept = Department.Dept_ID SQL statement entered through a program Output returned to another program OR screen Actual Data stored in the RDBMS ‘engine’
    • Some ‘Select’ statements
      • Select * from Employees;
      • Select LastName, JobDesc from Employees ;
      • Select distinct JobDesc from Employees;
    • ‘Where’ : to eliminate some rows
      • Select LastName, FirstName, JobDesc where JobDesc = ‘Clerk’;
      • Select LastName, FirstName, Salary where Salary > 35000;
      • Select LastName, Firstname where JobDesc <> ‘Clerk’;
    • Do you LIKE wildcards ?
      • Select LastName, FirstName, JobDesc from Employees where LastName like ‘D%';
      • Select LastName, FirstName, JobDesc from Employees where LastName like ‘_ _ _ _ i _’;
      • Select LastName, FirstName, JobDesc from Employees where LastName like ‘ _ _ _ _ i%’;
    • IN - BETWEEN
      • Select LastName, FirstName, JobDesc, Salary from Employees where Salary is between 10000 and 20000;
      • Select LastName, FirstName JobDesc, Dept from Employees where Dept in (’40’, ’20’, ’30’);
    • Order ! Order !!
      • Select LastName, FirstName, JobDesc, JoinDate from Employees ORDER BY JoinDate;
      • Select LastName, FirstName, JobDesc, Dept from Employees where Dept in (’20’,’30’,’40’) order by Dept;
    • More Restrictions : AND
      • Select LastName, FirstName, JobDesc, Salary, Dept from Employees where Dept = ’20’;
      • Select LastName, FirstName, JobDesc, Salary, Dept from Employees where Dept = ’20’ and Salary > 10000;
      • Select LastName, FirstName, JobDesc, Salary, Dept from Employees where Dept = ’20’ and Salary > 10000 and JobDesc = ‘Clerk’;
      • No rows returned !
    • Less Restrictions : OR
      • Select LastName, FirstName, JobDesc, JoinDate, Salary from Employees where JobDesc like ‘%Manager’ OR Salary >= 25000;
      • Select LastName, FirstName, JobDesc, JoinDate, Salary from Employees where JobDesc like ‘%Manager’ OR Salary >= 25000 OR FirstName = ‘Madhuri’
    • Matters of Precedence
      • Select LastName, FirstName, JobDesc, Salary where JobDesc = ‘Cus_Rep’ OR JobDesc = ‘Clerk’ AND Salary < 16000;
      • Select LastName, FirstName, JobDesc, Salary where (JobDesc = ‘Cus_Rep’ OR JobDesc = ‘Clerk’) and Salary < 16000;
    • Functions …
      • Two kinds of functions ….
        • Single row functions
          • Manipulate data items from one row
          • Accepts multiple arguments and return one value
            • Arguments can be column value or expression
          • Acts on one row
          • Returns one result per row
          • Can be nested
        • Multiple row functions
          • Works with data items from multiple rows
            • Arguments can be column value or expressions
          • Returns ONE result for a set of rows
    • Single Row Functions
      • Select EmpID, Salary, Comm, Salary+Comm*Salary from employee where DeptID = ’30’;
      • Select concat (‘Mr.’, Trunc(FirstName,1,1),’.’,LastName) from Employee where EmpID = ‘742866’;
      • Mr. A.Bacchan
    • Multiple Row Functions ..
      • Select sum(salary) from Employee;
      • Select avg(salary) from Employee;
      • Select max(salary) from Employee;
      • Other functions
        • Count
        • Min
        • And so on …
    • MultiRow Functions : Group By & Having
      • Select Dept, sum(salary) from Employee Group by Dept;
      • Select Dept, sum(salary) from Employee Group By Dept having Sum(salary) < 100000
    • Variety of Single Row Functions
      • Case manipulation
        • Lower
        • Upper
      • Character manipulation
        • Concat
        • Substr
        • Length
        • Lpad / Rpad
        • Etc
      • Number functions
        • Round
        • Trunc
        • Mod
      • Date Arithmetic
        • Months_Between
        • Next_Day
        • Add_Months
      • Data Type conversion
        • To_number
        • To_date
        • To_char
      This is only an illustrative subset. There are many more each with its own syntax ….
    • Join : accessing data from two tables
      • Select EmpID, LastName, FirstName, Location from Employee, Department where Employee.Dept = Department.Dept_ID
      • What happens if we miss the JOIN condition ?
    • Cartesian Product
      • Let us work with a slightly simpler table …
      • Select FirstName, LastName, DepartName from Employee, Department;
        • We get the Cartesian Product
        • This can lead to an enormously large number of rows
      • The JOIN condition is very important
    • The Outer Join
      • The common join is the Inner join
        • Where we pull out rows from both tables that have something ( the join key value ) in common
      • But what if there are rows in one table that do not have the common value … we use Outer Join
      • The syntax for performing an outer join in SQL is database-dependent. For example, in Oracle, we will place an &quot;(+)&quot; in the WHERE clause on the other side of the table for which we want to include all the rows.
    • Outer Join
      • Select FirstName, JobDesc, Dept_Name from Employee, Department where Employee.Dept (+) = Department.Dept_Id;
      • Select FirstName, JobDesc, Dept_Name from Employee, Department where Employee.Dept = Department.Dept_Id (+) ;
    • Full Outer JOIN
      • Select FirstName, JobDesc, Dept_Name from Employee, Full Outer Join Department where Employee.Dept = Department.Dept_Id;
    • SubQuery : An Alternate to JOIN
      • Select FirstName, JobDesc from Employee where Dept IN (Select Dept_ID from Department);
      • Select FirstName, JobDesc from Employee, Department where Employee.Dept = Department.Dept_ID
    • SELF JOIN : Joining a Table to itself
      • Suppose we have this table where we have an additional column called Manager that stores Manager ID
        • There is one person who does not report to anyone else.
        • Everyone else reports to department manager.
        • Department managers report to Executive
      • We want to know who report to whom
    • Self join
      • Select w.FirstName, w.JobDesc, w.Dept , ’reports to’, m.FirstName,m.JobDesc,m.Dept from Employee w , Employee m where w.Manager = m.EmpID
    • Insert ..
      • Insert into Department (’50’,’Research’,’123456’,’Pune’)
    • Update ..
      • Update Department set Manager = ‘123456’ where Dept_ID = ’40’;
    • Delete ..
      • Delete from Department where DeptID = ’40’;
    • Create View
      • Create View myView as (
        • Select .....
        • any SQL query
        • simple or complex
        • that we have
        • learnt about so far ..
        • )
      • Select * from myView ;