0% found this document useful (0 votes)
18 views

SQL - NOTES

SQL notes

Uploaded by

manickambruno750
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL - NOTES

SQL notes

Uploaded by

manickambruno750
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

XML (Extensible Markup Language) HTML (Hypertext Markup Language)

 XML tags are user defined.  HTML tags are pre-defined.


 XML stores and transfers data.  HTML is about displaying data.
 Dynamic in nature  Static in nature.
 Case sensitive  Not case sensitive.
Binary File : CSV FILE :
 Extension is .dat  Extension is .csv
 Not human readable  Human readable
 Stores data in the form of 0s and 1s.  Stores data like a Text file.
DDL : DML :
 Data Definition Language  Data Manipulation Language
 Commands for defining relation  Commands for manipulating Tuples in
schemas, deleting relations , modifying databases.
relation schemas and creating indexes.  E.g: Insert, Update , select , Delete
 E.g : Create, Alter , Drop
Char(n) : Varchar(n) :
 Stores a fixed length string between 1  Stores a variable length string
and 255 characters.  No blanks are added even if value is of
 If the value is of smaller length, adds smaller length
blank spaces.  No wastage of space
 Some space is wasted.
Equi-Join : Natural Join :
 The join in which columns from two  The join in which only one of the
tables are compared for equality. identical columns existing in both tables
 Duplicate columns are shown. is present.
 No duplication of columns.
Circuit Switching : Packet Switching :
 Dedicated communication path is  No dedicated communication path is
established between the sender and the established from the source to the
receiver. destination.
 Data is processed and transmitted at the  Data is processed and transmitted , not
source only. only at the source but at each switching
 It is more reliable. station.
 It is less reliable.
Local Variable : Global Variable :
 It is a variable which is declared  It is a variable which is declared
within a function or within a block. outside all the functions or in a global
 It cannot be accessed outside the space.
function but only within a  It is accessible throughout the
function/block of a program. program in which it is declared

(?) Differentiate between COUNT() and COUNT (*) functions in SQL with appropriate example.
COUNT(*) returns the count of all rows in the table.
COUNT() is used with Column_Name passed as argument and counts the number of non-NULL
values in a column.
Example : Table : EMPL e.g.SELECT COUNT(*) FROM EMPL ; COUNT(*)
EMPN ENAM JOB 3
O E e.g. SELECT COUNT(JOB) FROM EMPL ;
100 AAA CLERK
200 BBB NULL COUNT(JOB)
300 CCC MANAGE 2
R Since JOB has 1 NULL values

(?)Explain the use of “Foreign key” in a Relational Database Management System. Give
example ?
A Foreign Key is used to represent a relationship between two tables in a database. Its value is
derived from the Primary key attribute of another Table. For Example :
Table : Trainer Table : Course
TID TNAME CITY
CID CNAME TID
101 AAA DELHI
C1 DDD 101
102 BBB MUMBAI
C2 EEE 102
103 CCC GOA C3 FFF 103
In the tables TRAINER and COURSE given above , TID is primary key in TRAINER table but foreign
key in COURSE table.
(?)Differentiate between the terms Attribute and Domain.
Attribute : Domain :
The column of a Table is known as an Attribute. It is a set of permissible values from which
attributes can take required value.
For Example : Table : Student

Name Class Marks


AAA XII 90
BBB X 99
Name , Class and Marks are Attributes. The attribute CLASS has domain of X , XII

(?)Database RESULT SET : The RESULT SET refers to a logical set of records that are fetched from the
database by executing an SQL query and made available to the application program.
(?)Database CURSOR : A Database Cursor is a special control structure that facilitates the row by row
processing of records in the resultset.
(?)Fetchall() :
 It will return all the rows from the resultset in the form of a Tuple containing the records.
 An empty list is returned if there is no record to fetch the cursor.
(?)Fetchone() :
 It will return one record from the resultset as a tuple.
 If there are no more records then it returns NONE.
(?)Fetchmany(<n>) :
This method accepts number of records to fetch and returns a tuple where each record itself
is a tuple. If there are not more records then it returns an empty typle.
(?)Rowcount :
The rowcount is a property of cursor object that returns the number of rows retrieved from
the cursor so far.
(?)CONSTRAINTS : A constraint is a condition or check applicable on a field or set of fields.
For example: NOT NULL – This constraint makes that a column cannot have a NULL value.
UNIQUE – This constraint makes that all values in a column are different.
PRIMARY KEY – A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table.
DEFAULT – A default value can be specified for a column using the DEFAULT clause.
CHECK – This constraint limits values that can be inserted into a column of a table.
FOREIGN KEY - Its value is derived from the Primary key attribute of another Table.
(?)JOINS : A join is a query that combines rows from two or more tables, based on a related column
between them.
Types of Joins : (a) Cross join / Cartesian Product (b) Equi Join (c) Natural Join
(?)CARTESIAN PRODUCT / CROSS JOIN :
All rows in the first table are concatenated with all the rows in the second table.
No. of Rows in resultant table = No. of Rows in first table X No. of Rows in second table.
No. of Columns in resultant table = No. of Columns in first table + No. of columns in second table.
For Example: SELECT * FROM TABLE1 , TABLE2 ;
Table1 Table2
ROLL NAME ROLL CLASS
ROLL NAME ROLL CLASS 10 AA 10 11
10 AA 10 11 10 AA 20 12
20 BB 20 12 20 BB 10 11
20 BB 20 12
(?)NATURAL JOIN :
The join in which only one of the identical columns ( coming from joined tables ) exists , is called
Natural Join. For Example: SELECT * FROM TABLE1 NATURAL JOIN TABLE2 ;
Table1 Table2
ROLL NAME ROLL CLASS ROLL NAME CLASS
10 AA 10 11 10 AA 11
20 BB 20 12 20 BB 12
(?)EQUI JOIN : The join, in which columns are compared for equality, is called Equi-Join.
For Example:
SELECT Table1.ROLL , NAME ,CLASS
Table1 Table2 FROM TABLE1 , TABLE2
ROLL NAME ROLL CLASS WHERE TABLE1.ROLL = TABLE2.ROLL ;
10 AA 10 11
ROLL NAME CLASS
20 BB 20 12
10 AA 11
20 BB 12
(?)AGGREGATE FUNCTIONS : An aggregate function in SQL performs a calculation on multiple
values and returns a single value. For example : SUM() , AVG() , COUNT() , MIN() , MAX().

(?)RELATION : A relation is a Table (i.e.,) data arranged in rows and columns.


For example : Table : Student
NAM
CLASS SEC
E
AAA VI A
BBB VII B
(?)TUPLE : The Rows of Tables (Relations) are generally referred to as TUPLES.
For example : Table : STUDENT
NAM
CLASS SEC
E
AAA VI A
BBB VII B
Number of Tuples = 2.

(?)DEGREE : Number of Columns or Attributes or Fields in a Table are called table’s DEGREE.
CARDINALITY : Number of Rows or Tuples or Records in a Table are called table’s CARDINALITY.
For example : Table : STUDENT
NAM
CLASS SEC
E
AAA VI A
BBB VII B
Number of Degree (columns) = 3 Number of Cardinality (Rows) = 2
(?)PRIMARY KEY :
It is the set of one or more attributes that can uniquely identify tuples within a relation.
COMPOSITE PRIMARY KEY : When a Primary Key is made up of two or more attributes, it is called
Composite Primary Key.
CANDIDATE KEY : A candidate key is the one that is capable of becoming the primary key.
ALTERNATE KEY : It is a candidate key which is not the primary key.
For example : Table : STUDENT
ADMN ROLL NAM
MARKS
O E
A10 10 AAA 50
A20 20 BBB 60
Candidate keys : Admno , Roll
Primary key : Admno
Alternate key : Roll
(?) INT : INT contains integral values and it occupies till 4 bytes of storage.
FLOAT : Float contains a number with decimal point values and it occupies till 8 bytes of storage.
DATE : ‘YYYY-MM-DD’ format which ranges from ‘1000-01-01’ to ‘9999-12-31’.
(?)Is MySQL case sensitive ? Explain.
No, MySQL is not case sensitive. But it is case sensitive in the case of patterns enclosed within
single (‘ ’) or double(“ ”) quotation marks.
(?)ALIAS : The term ALIAS means providing an attribute or a table with another temporary name in
a query. For this AS keyword is used.
For example : SELECT SNAME AS “STUDENT NAME” FROM SCHOOL ;
In the output of the above query, the attribute SNAME will be displayed as STUDENT NAME.
WHERE CLAUSE HAVING CLAUSE
It is used to apply conditions on individual rows It is used to apply conditions on a group of rows
It selects rows before grouping It selects rows after grouping
It cannot contain aggregate functions It can contain aggregate functions
Example: SELECT * FROM CUSTOMER Example: SELECT CITY ,SUM(SALARY) FROM
WHERE SALARY > 2000 ; CUSTOMER GROUP BY CITY HAVING CITY=“DELHI” ;
(?)GROUP BY : The GROUP BY clause combines all those records that have identical values in a
particular field or a group of fields.The GROUP BY statement is often used with aggregate functions.
Example: SELECT CITY , SUM(SALARY) FROM CUSTOMER GROUP BY CITY ;
ALTER TABLE UPDATE
 Used to change or modify the structure of the Table  Used to change or modify the data in the Table.
 It is a DDL command  It is a DML command
 It can not be rolled back  It can be rolled back
(?) NULL : NULL means Absence of value. NULL cannot be compared using relational operator. The
statement IS NULL , IS NOT NULL is used to check whether column contains NULL or not.
The following query selects details of all those employees who have not been given a bonus.
mysql > SELECT * FROM EMPLOYEE WHERE BONUS IS NULL ;
ENAME SALARY BONUS
AAA 15000 NULL
BBB 20000 NULL
(?)LIKE :SQL provides a LIKE operator that can be used with the WHERE clause to search for a specified
pattern in a column. The LIKE operator makes use of the following two wild card characters.
 % (per cent) – used to represent zero , one or multiple characters.
 _ (underscore) – used to represent exactly a single character.
The following query selects details of all those students whose name starts with ‘A’.
mysql> SELECT * FROM STUDENT WHERE SNAME LIKE ‘A%’ ;
SNAME CLASS SEC
AAA VI A
ABB VII B

You might also like