BASIC STRUCTURE OF SQL
PREPARED BY
MS.A.HAMEEDHA - III-B.SC - INFORMATION TECHNOLOGY
UNDER THE GUIDANCE OF
MRS.P.ANUSHA M.SC(IT).,M.PHIL.,D.P.T.T.,(PH.D).,
ASSISTANT PROFESSOR,
DEPARTMENT OF INFORMATION TECHNOLOGY,
BON SECOURS COLLEGE FOR WOMEN,
THANJAVUR.
WHAT IS SQL
• Structure query language(SQL) is a database query language used for storing
and managing data in relational DBMS.
• SQL was the first commercial language introduced for E.F.Codd’s relational
model of database. Today almost all rdbms (mysql, oracle, infomix, sybase, MS
access) use SQL as the standard database query language. SQL is used to
perform all types of data operations in RDBMS.
NAME STUD_ID AGE MARK1 MARK2
AAA 101 18 98 88
BBB 102 19 95 87
CCC 103 19 89 89
ENTITY : STUDENT.
FUNDAMENTAL SET OF SQL QUERY
• Select
It is used to list the attributes desired in the result of query.
• From
It lists the relations to be scanned in the evaluation of the expression.
• Where
It consists of a predicate involving attributes of the relations that appear in
the from clause.
SELECT CLAUSE
• The select clause corresponds to the projection operation of the relational
algebra.The asterisk symbol “*” can be used to denote all attributes.
• The select statement is used to select data from a database.
• The data returned is stored in a result table called result_set.
• Syntax
Select * from table name;
• Example:
Select * from student;
OUTPUT:
FROM CLAUSE
• The FROM command is used to specify which table to select or delete data from.
• The from clause by itself defines a cartesian product of the relations in the clause
• Syntax:
Select column name from table name;
• Example:
Select stud_id from student;
OUTPUT:
STUD_ID
101
102
103
WHERE CLAUSE
• SQL includes a between comparison operator to simplify where clauses that
specify that the value be less than or equal to some value and greater than or
equal to some other value.
• Syntax:
Select column 1,column 2
From table name
Where condition;
• Example:
Select mark1,mark2
From student
Where stud_id =‘101’;
OUTPUT:
MARK1 MARK2
98 88
OPERATORS USED IN WHERE CLAUSE
OPERATORS DESCRIPTION
= Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
<> Not equal to
Between Between a certain range
In to specify multiple possible values for a column
ENTITY 1: BORROWER ENTITY 2: LOAN
LOAN_NUMBER CUSTOMER_NAM
E
111 ABI
112 XAVIER
113 SEETHA
LOAN_NUMBER AMOUNT
111 100000
112 150000
113 250000
RENAME OPERATION
• SQL provides a mechanism for renaming both relations and attributes.It uses the
clause,taking the form:
Old_name as new_name;
• The as clause appear in both the select and from clauses.
• For example,if you want the attribute name LOAN_NUMBER to be replaced with
the name LOAN_ID,we can rewrite the query as
Select customer_name,borrower.Loan_number as loan_id,amount
From borrower,loan
Where borrower.Loan_number=loan.Loan_number
TUPLE VARIABLES
• A tuple variable in SQL must be associated with a particular relation.Tuple
variables are defined in the from clause by way of the as clause.
• Syntax:
Select column name,T.Cn ,S.Cn
From table name as T, table name as S
Where T.Column name = S.Column name;
• Example:
Select customer_name,T.Loan_number,S.Amount
From borrower as T, loan as S
Where T.Loan_number=S.Loan number;
STRING OPERATION
• SQL specifies strings by enclosing them in single quotes,for example,’student‘.A
single quote character that is part of a string can be specified by using two single
quote characters.
• The most commonly used operation on strings is pattern by using two special
characters:
• Percent(%): the % character matches any substring.
• Underscore (_): the _ character matches any character.
ORDERING THE DISPLAY OF TUPLES
• The order by clause causes the tuples in the result of a query to appear in sorted
order.
• To specify the sort order,we may specify desc for descending order or asc for
ascending order.
• Example:
Select * from loan
Order by amount desc, loan_number asc;

basic structure of SQL FINAL.pptx

  • 1.
  • 2.
    PREPARED BY MS.A.HAMEEDHA -III-B.SC - INFORMATION TECHNOLOGY UNDER THE GUIDANCE OF MRS.P.ANUSHA M.SC(IT).,M.PHIL.,D.P.T.T.,(PH.D)., ASSISTANT PROFESSOR, DEPARTMENT OF INFORMATION TECHNOLOGY, BON SECOURS COLLEGE FOR WOMEN, THANJAVUR.
  • 3.
    WHAT IS SQL •Structure query language(SQL) is a database query language used for storing and managing data in relational DBMS. • SQL was the first commercial language introduced for E.F.Codd’s relational model of database. Today almost all rdbms (mysql, oracle, infomix, sybase, MS access) use SQL as the standard database query language. SQL is used to perform all types of data operations in RDBMS.
  • 4.
    NAME STUD_ID AGEMARK1 MARK2 AAA 101 18 98 88 BBB 102 19 95 87 CCC 103 19 89 89 ENTITY : STUDENT.
  • 5.
    FUNDAMENTAL SET OFSQL QUERY • Select It is used to list the attributes desired in the result of query. • From It lists the relations to be scanned in the evaluation of the expression. • Where It consists of a predicate involving attributes of the relations that appear in the from clause.
  • 6.
    SELECT CLAUSE • Theselect clause corresponds to the projection operation of the relational algebra.The asterisk symbol “*” can be used to denote all attributes. • The select statement is used to select data from a database. • The data returned is stored in a result table called result_set. • Syntax Select * from table name; • Example: Select * from student;
  • 7.
  • 8.
    FROM CLAUSE • TheFROM command is used to specify which table to select or delete data from. • The from clause by itself defines a cartesian product of the relations in the clause • Syntax: Select column name from table name; • Example: Select stud_id from student;
  • 9.
  • 10.
    WHERE CLAUSE • SQLincludes a between comparison operator to simplify where clauses that specify that the value be less than or equal to some value and greater than or equal to some other value. • Syntax: Select column 1,column 2 From table name Where condition; • Example: Select mark1,mark2 From student Where stud_id =‘101’;
  • 11.
  • 12.
    OPERATORS USED INWHERE CLAUSE OPERATORS DESCRIPTION = Equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to <> Not equal to Between Between a certain range In to specify multiple possible values for a column
  • 13.
    ENTITY 1: BORROWERENTITY 2: LOAN LOAN_NUMBER CUSTOMER_NAM E 111 ABI 112 XAVIER 113 SEETHA LOAN_NUMBER AMOUNT 111 100000 112 150000 113 250000
  • 14.
    RENAME OPERATION • SQLprovides a mechanism for renaming both relations and attributes.It uses the clause,taking the form: Old_name as new_name; • The as clause appear in both the select and from clauses. • For example,if you want the attribute name LOAN_NUMBER to be replaced with the name LOAN_ID,we can rewrite the query as Select customer_name,borrower.Loan_number as loan_id,amount From borrower,loan Where borrower.Loan_number=loan.Loan_number
  • 15.
    TUPLE VARIABLES • Atuple variable in SQL must be associated with a particular relation.Tuple variables are defined in the from clause by way of the as clause. • Syntax: Select column name,T.Cn ,S.Cn From table name as T, table name as S Where T.Column name = S.Column name; • Example: Select customer_name,T.Loan_number,S.Amount From borrower as T, loan as S Where T.Loan_number=S.Loan number;
  • 16.
    STRING OPERATION • SQLspecifies strings by enclosing them in single quotes,for example,’student‘.A single quote character that is part of a string can be specified by using two single quote characters. • The most commonly used operation on strings is pattern by using two special characters: • Percent(%): the % character matches any substring. • Underscore (_): the _ character matches any character.
  • 17.
    ORDERING THE DISPLAYOF TUPLES • The order by clause causes the tuples in the result of a query to appear in sorted order. • To specify the sort order,we may specify desc for descending order or asc for ascending order. • Example: Select * from loan Order by amount desc, loan_number asc;