UNIT 3.3 SQL COMMANDS
INTRODUCTION :: My SQL is RDBMS that uses SQL to access
the data. It is downloaded from www.mysql.org.MySQL was
invented by Michael Widenius. The logo of My SQL is ,the
Dolphin.SQL stands for Structured Query Language. Initially SQL
was called Sequel.
MEANING :: SQL is simple query language used for accessing,
managing and handling data in relation database.
CLASSIFICATION OF SQL STATEMENTS :: SQL commands
can be divided into following categories.
1. Data Definition Language(DDL)
2. Data Manipulation Language(DML)
3. Data Control Language(DCL)
4. Transaction Control Language(TCL)
Data Definition language(DDL) :: (Defines storage structure.)
 Create
 Alter
 Drop
 Truncate
Data Manipulation Language(DML):: (Used to access and
manipulate data.)
 Select
 Insert
 Update
 Delete
Data Control Language(DCL)::
 Grant
 Revoke
Transaction Control Language(TCL)::
 Commit
 Rollback
 Savepoint
DATA DICTIONARY:: Data Dictionary contains data about
database. Data Dictionary contains information about tables. It
contains result of compilation of DDL statements.
Metadata means data about data.
Concept of Data type:: Data types indicate the type of value
the field will contain.
String, Character and date must always be enclosed in
single quotes/double quotes.
Difference between Char and Varchar :: Char refers to
Character and Varchar refers to Variable Character.
Char(7) – It always take 7 bytes of data whether you are
storing one character or 7 character. There is wastage of
space. Char is fixed length data type.
Varchar(10) – If you are storing one character then it will take
only one byte and if you are storing ten character then it will
take ten bytes. There is no wastage of space. Extra space is
automatically removed. Varchar is variable length data type.
Null Values :: If a column has no value then column is said to
be null. If column is assign as primary key then it must not
Null.
Comments :: Comments used in SQL are
/* text */ (for multiple lines)
# (for single lines)
-- (for single lines) and must be followed by a space
SQL Command Syntax ::
Keyword(select , insert…….)
Statements
Clauses(from , where)
Argument(where sales=5000) sales=5000 is an argument.
Grouping Result : Group by clause :: Group by clause
is used to divide the table into groups.
Table::emp
select job , count(*) , sum(comm)
from emp
group by job;
emp no empname Job hiredate sal comm deptno
7839 King President 17-Nov-81 5000 NULL
7698 Blake Manager 01-May-81 2850 80
7782 Clark Manager 09-June-81 2800 NULL
7566 Jones Manager 12-Apr-81 2450 21
7564 Martin Salesman 28-Sep-81 1250 1400 30
7499 Allen Salesman 20-Sep-81 1600 300 30
job count(*) sum(comm)
President 1 0
Manager 3 0
Salesman 2 1700
Placing condition on groups – having clause
select grade , avg(gross) , sum(gross)
from employee
group by grade
having grade=‘E2’;
grade avg(gross) sum(gross)
E2 8500 17000
Select job , count(*)
from emp
group by job
having count(*)<3;
Functions used in SQL ::
Avg :: Compute average value
Min :: Find minimum value
Max :: Find maximum value
job count(*)
Presidnt 1
Salesman 2
Sum :: Find total value
Count :: to count NOT NULL values in a column
Count(*) :: to count total number of rows in a table
Joins:: A join is a query that combines rows from two
or more tables. In a join query , more than one table
are listed in from clause.
View Command ::
View is a Virtual table with no data. If data is changing in
underlying table, the same change is reflect in view.
create view taxpayee as
select * from employee
where gross>8000;
taxpayee is a view(virtual table) that contain details of those
employees from employee table that have gross > 8000
Select * from taxpayee;
This query dislays all rows present in view.
If any column in the view is to be given a different name
other than the name of the column from which it is delivered,
it is done as follows.
create view taxpayee
(empcode, empname, sex, empgrade, empgross) as
select * from employee
where gross > 8000;
create view taxpayee(ecode,ename,tax) as
select ecode,ename,gross*.1
from employee
Where gross > 8000;
The above created view has an additional column tax
which is 10% of the gross
You can use insert , delete , update command on view
Assignment – 5
Consider the following tables games and players and answer (a) and (b) part of this
question.
TABLE :: Games
TABLE :: Players
Gcode Gname Type Number PrizeMoney Scheduled date
101 Carrom Board Indoor 2 5000 23-Jan-2004
102 Badminton Outdoor 2 12000 12-Dec-2003
103 Table Tennis Indoor 4 8000 14-Feb-2004
105 Chess Indoor 2 9000 1-Jan-2004
108 Lawn Tennis Outdoor 4 25000 19-Mar-2004
Pcode Name Gcod
e
1 Nabi Ahmad 101
2 Ravi Sahai 108
3 Jatin 101
4 Nazreen 103
(a) Write SQL commands for the following statement
(i) To display the name of all Games with their Gcode.
(ii) To display details of those Games which are having PrizeMoney
more than 7000
(iii) To display the content of the Games table in ascending order of
Schedule date
(iv) To display sum of PrizeMoney for each type of Games.
(b) Give the output of the following SQL Queries
(i) Select count(distinct Number) from Games;
(ii) Select max(Schedule date), min(Schedule date);
(iii) Select Name Gname from Game G Players P
Where G.Gcode=P.Gcode and G.PrizeMoney>10000;
(iv) Select distinct Gcode from Players;
Assignment - 7
SQL (1).pptx

SQL (1).pptx

  • 1.
    UNIT 3.3 SQLCOMMANDS INTRODUCTION :: My SQL is RDBMS that uses SQL to access the data. It is downloaded from www.mysql.org.MySQL was invented by Michael Widenius. The logo of My SQL is ,the Dolphin.SQL stands for Structured Query Language. Initially SQL was called Sequel. MEANING :: SQL is simple query language used for accessing, managing and handling data in relation database. CLASSIFICATION OF SQL STATEMENTS :: SQL commands can be divided into following categories. 1. Data Definition Language(DDL) 2. Data Manipulation Language(DML)
  • 2.
    3. Data ControlLanguage(DCL) 4. Transaction Control Language(TCL) Data Definition language(DDL) :: (Defines storage structure.)  Create  Alter  Drop  Truncate Data Manipulation Language(DML):: (Used to access and manipulate data.)  Select  Insert  Update  Delete
  • 3.
    Data Control Language(DCL):: Grant  Revoke Transaction Control Language(TCL)::  Commit  Rollback  Savepoint DATA DICTIONARY:: Data Dictionary contains data about database. Data Dictionary contains information about tables. It contains result of compilation of DDL statements. Metadata means data about data. Concept of Data type:: Data types indicate the type of value the field will contain.
  • 5.
    String, Character anddate must always be enclosed in single quotes/double quotes. Difference between Char and Varchar :: Char refers to Character and Varchar refers to Variable Character. Char(7) – It always take 7 bytes of data whether you are storing one character or 7 character. There is wastage of space. Char is fixed length data type. Varchar(10) – If you are storing one character then it will take only one byte and if you are storing ten character then it will take ten bytes. There is no wastage of space. Extra space is automatically removed. Varchar is variable length data type.
  • 6.
    Null Values ::If a column has no value then column is said to be null. If column is assign as primary key then it must not Null. Comments :: Comments used in SQL are /* text */ (for multiple lines) # (for single lines) -- (for single lines) and must be followed by a space SQL Command Syntax :: Keyword(select , insert…….) Statements Clauses(from , where) Argument(where sales=5000) sales=5000 is an argument.
  • 34.
    Grouping Result :Group by clause :: Group by clause is used to divide the table into groups. Table::emp select job , count(*) , sum(comm) from emp group by job; emp no empname Job hiredate sal comm deptno 7839 King President 17-Nov-81 5000 NULL 7698 Blake Manager 01-May-81 2850 80 7782 Clark Manager 09-June-81 2800 NULL 7566 Jones Manager 12-Apr-81 2450 21 7564 Martin Salesman 28-Sep-81 1250 1400 30 7499 Allen Salesman 20-Sep-81 1600 300 30
  • 35.
    job count(*) sum(comm) President1 0 Manager 3 0 Salesman 2 1700 Placing condition on groups – having clause select grade , avg(gross) , sum(gross) from employee group by grade having grade=‘E2’; grade avg(gross) sum(gross) E2 8500 17000
  • 36.
    Select job ,count(*) from emp group by job having count(*)<3; Functions used in SQL :: Avg :: Compute average value Min :: Find minimum value Max :: Find maximum value job count(*) Presidnt 1 Salesman 2
  • 37.
    Sum :: Findtotal value Count :: to count NOT NULL values in a column Count(*) :: to count total number of rows in a table Joins:: A join is a query that combines rows from two or more tables. In a join query , more than one table are listed in from clause.
  • 47.
    View Command :: Viewis a Virtual table with no data. If data is changing in underlying table, the same change is reflect in view. create view taxpayee as select * from employee where gross>8000; taxpayee is a view(virtual table) that contain details of those employees from employee table that have gross > 8000 Select * from taxpayee; This query dislays all rows present in view.
  • 48.
    If any columnin the view is to be given a different name other than the name of the column from which it is delivered, it is done as follows. create view taxpayee (empcode, empname, sex, empgrade, empgross) as select * from employee where gross > 8000; create view taxpayee(ecode,ename,tax) as select ecode,ename,gross*.1 from employee Where gross > 8000;
  • 49.
    The above createdview has an additional column tax which is 10% of the gross You can use insert , delete , update command on view
  • 50.
    Assignment – 5 Considerthe following tables games and players and answer (a) and (b) part of this question. TABLE :: Games TABLE :: Players Gcode Gname Type Number PrizeMoney Scheduled date 101 Carrom Board Indoor 2 5000 23-Jan-2004 102 Badminton Outdoor 2 12000 12-Dec-2003 103 Table Tennis Indoor 4 8000 14-Feb-2004 105 Chess Indoor 2 9000 1-Jan-2004 108 Lawn Tennis Outdoor 4 25000 19-Mar-2004 Pcode Name Gcod e 1 Nabi Ahmad 101 2 Ravi Sahai 108 3 Jatin 101 4 Nazreen 103
  • 51.
    (a) Write SQLcommands for the following statement (i) To display the name of all Games with their Gcode. (ii) To display details of those Games which are having PrizeMoney more than 7000 (iii) To display the content of the Games table in ascending order of Schedule date (iv) To display sum of PrizeMoney for each type of Games. (b) Give the output of the following SQL Queries (i) Select count(distinct Number) from Games; (ii) Select max(Schedule date), min(Schedule date); (iii) Select Name Gname from Game G Players P Where G.Gcode=P.Gcode and G.PrizeMoney>10000; (iv) Select distinct Gcode from Players;
  • 52.