unit- 3 is a database management system, like sql server, oracle, informix, postgres, etc. mysql is...
TRANSCRIPT
Unit- 3INTRODUCTION TO SQL
2
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
SQL fully abbreviated as Structured Query Language.
Is a domain -specific language used to manage the relational databases and
performs different operations on the data stored in them.
SQL is used as their standard database language by all the relational database
management systems like Oracle, Informix, Posgres, SQL server, MySQL, MS
Access, and Sybase.
Introduction to SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
2
SQL is easy to learn.
SQL is used to access data from relational database management systems.
SQL can execute queries against the database.
SQL is used to describe the data.
SQL is used to define the data in the database and manipulate it when needed.
SQL is used to create and drop the database and table.
SQL is used to create a view, stored procedure, function in a database.
SQL allows users to set permissions on tables, procedures, and views
Characteristics of SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
3
High speed: Using the SQL queries, the user can quickly and efficiently
retrieve a large amount of records from a database.
No coding needed: In SQL, it is very easy to manage the database system. It
doesn't require a substantial amount of code to manage the database system.
Well defined standards: Long established are used by the SQL databases
that are being used by ISO and ANSI.
Portability: SQL used in laptop, PCs, server and even some mobile phones.
Interactive language: SQL is a domain language used to communicate with
the database.
Multiple data view: Using the SQL language, the users can make different
views of the database structure
Advantages of SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
4
SQL DATA TYPE
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
5
1. Binary Data types:
2. Approximate Numeric Data type:
Data Type Description
binary It has a maximum length of 8000 bytes. It contains fixed-length binary data.
varbinary It has a maximum length of 8000 bytes. It contains variable-length binary data.
image It has a maximum length of 2,147,483,647 bytes.
Data type From To Description
float -1.79E + 308 1.79E + 308It is used to specify a floating-point value e.g. 6.2,2.9 etc.
real -3.40e + 38 3.40E + 38 It specifies a single precision floating point number
3. Exact Numeric Data type:
4. Date and time Data type
SQL DATA TYPE
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
6
Data type Descriptionint It is used to specify an integer value.
smallint It is used to specify small integer value.bit It has the number of bits to store.
decimal It specifies a numeric value that can have a decimal number.
numeric It is used to specify a numeric value.
Datatype Descriptiondate It is used to store the year, month, and days value.time It is used to store the hour, minute, and second values.
timestamp It stores the year, month, day, hour, minute, and the second value.
5. Character String Data type:
SQL DATA TYPE
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
7
Data type Description
charIt has a maximum length of 8000 characters. It containsFixed-length non-unicode characters.
varcharIt has a maximum length of 8000 characters. It containsvariable-length non-unicode characters.
textIt has a maximum length of 2,147,483,647 characters. Itcontains variable-length non-unicode characters.
MySQL is a fast, easy to use relational database. It is currently the most
popular open-source database.
It is very commonly used in conjunction with PHP scripts to create powerful
and dynamic server-side applications.
MySQL is used for many small and big businesses.
It is developed, marketed and supported by MySQL AB, a Swedish company.
It is written in C and C++.
What is MySQL?
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
8
MySQL is an open-source database so you don't have to pay a single penny touse it.MySQL is a very powerful program so it can handle a large set of functionalityof the most expensive and powerful database packages.MySQL is customizable because it is an open source database and the open-source GPL license facilitates programmers to modify the SQL softwareaccording to their own specific environment.MySQL is quicker than other databases so it can work well even with the largedata set.MySQL supports many operating systems with many languages like PHP,PERL, C, C++, JAVA, etc.MySQL uses a standard form of the well-known SQL data language.MySQL is very friendly with PHP, the most popular language for webdevelopment.MySQL supports large databases, up to 50 million rows or more in a table.The default file size limit for a table is 4GB.
Why MYSQL is popular?
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
9
1. Relational Database Management System: MySQL is a RDBMS System.
2. Easy to use: MySQL is easy to use.
3. It is secure: MySQL consist of a solid data security layer that protects
sensitive data from intruders. Passwords are encrypted in MySQL.
4. Client/ Server Architecture: MySQL follows a client /server architecture.
There is a database server & arbitrarily many clients, which communicate
with the server;
5. Free to download: MySQL is free to use and you can download it from
MySQL official website.
6. It is scalable: MySQL can handle almost any amount of data, up to as much
as 50 million rows or more. The default file size limit is about 4 GB.
MySQL Features
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
10
7. Compatible on many operating systems: MySQL is compatible to run on
many operating systems, like Novell NetWare, Windows* Linux*, many
varieties of UNIX.
8. Allows roll-back: MySQL allows transactions to be rolled back, commit and
crash recovery.
9. High Performance: MySQL is faster, more reliable and cheaper because of its
unique storage engine architecture.
10. High Flexibility: MySQL supports a large number of embedded
applications which makes MySQL very flexible.
11. High Productivity: MySQL uses Triggers, Stored procedures and views
which allows the developer to give a higher productivity.
MySQL Features
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
11
MySQL version less than 5.0 doesn't support ROLE, COMMIT and stored
procedure.
MySQL does not support a very large database size as efficiently.
MySQL doesn't handle transactions very efficiently and it is prone to data
corruption.
MySQL is accused that it doesn't have a good developing and debugging tool
compared to paid databases.
MySQL doesn't support SQL check constraints.
Disadvantages / Drawback of MySQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
12
MySQL Data Types
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
13
1. Numeric Data TypeData Type Description
INTA normal-sized integer that can be signed or unsigned. If signed, the range is from -2147483648 to 2147483647. If unsigned, the range is from 0 to 4294967295.
TINYINTA very small integer that can be signed or unsigned. If signed, the range is from -128to 127. If unsigned, the range is from 0 to 255.
SMALLINTA small integer that can be signed or unsigned. If signed, the range is from -32768 to32767. If unsigned, the range is from 0 to 65535.
MEDIUMINT A medium-sized integer that can be signed or unsigned..
BIGINT A large integer that can be signed or unsigned.
FLOAT(m,d)A floating-point number that cannot be unsigned. You can define the display length(m) and the number of decimals (d).
DOUBLE(m,d)A double precision floating-point number that cannot be unsigned. You can define thedisplay length (m) and the number of decimals (d)
DECIMAL(m,d)An unpacked floating-point number that cannot be unsigned. Defining the displaylength (m) and the number of decimals (d) is required.
2. Date and Time Data Type:
MySQL Data Types
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
14
Data Type Maximum Size Explanation
DATERange from '1000-01-01' to '9999-12-31'.
Displayed as 'yyyy-mm-dd'.
DATETIMEValues range from '1000-01-0100:00:00' to '9999-12-31 23:59:59'.
Displayed as 'yyyy-mm-ddhh:mm:ss'.
TIMESTAMP(m)
Values range from '1970-01-0100:00:01' UTC to '2038-01-1903:14:07' TC.
Displayed as 'YYYY-MM-DD HH:MM:SS'.
TIMEValues range from '-838:59:59' to'838:59:59'.
Displayed as 'HH:MM:SS'.
YEAR[(2|4)] Year value as 2 digits or 4 digits. Default is 4 digits.
3. String Data Types:
MySQL Data Types
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
15
Data Type Maximum Size
CHAR(size) Maximum size of 255 characters.
VARCHAR(size) Maximum size of 255 characters.
TINYTEXT(size) Maximum size of 255 characters.
TEXT(size) Maximum size of 65,535 characters.
MEDIUMTEXT(size) Maximum size of 16,777,215 characters.
LONGTEXT(size) Max size of 4GB or 4,294,967,295 characters.
BINARY(size) Maximum size of 255 characters.
VARBINARY(size) Maximum size of 255 characters.
MySQL Data Types
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
16
4. Large Object Data Types (LOB) Data Types.
Data Type Syntax Maximum Size
TINYBLOB Maximum size of 255 bytes.
BLOB(size) Maximum size of 65,535 bytes.
MEDIUMBLOB Maximum size of 16,777,215 bytes.
LONGTEXT Maximum size of 4gb or 4,294,967,295 characters.
SQL Vs MySQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
17
SQL stands for Structured Query Language. It’s a standard language for
accessing and manipulating databases.
MySQL is a database management system, like SQL Server, Oracle, Informix,
Postgres, etc.
MySQL is an RDMS (Relational Database Management System).
When considering a utility for data management the two most popular
choices are MySQL and SQL Server.
Both are efficient at keeping your data organized and readily available
through a user interface.
Both technologies have the concept of the schema for data storage.
An operator is a reserved word or a character used primarily in an SQL
statement's WHERE clause to perform operation(s), such as comparisons and
arithmetic operations.
These Operators are used to specify conditions in an SQL statement and to
serve as conjunctions for multiple conditions in a statement.
There are various types of SQL operator:
SQL OPERATOR
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
18
Let's assume 'variable a' and 'variable b'. Here, 'a' contains 20 and 'b' contains
10.
SQL Arithmetic Operators
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
19
Operator Description Example+ It adds the value of both operands. a+b will give 30
-It is used to subtract the right-hand operand from theleft-hand operand.
a-b will give 10
* It is used to multiply the value of both operands. a*b will give 200
/It is used to divide the left-hand operand by the right-hand operand.
a/b will give 2
%It is used to divide the left-hand operand by the right-hand operand and returns reminder.
a%b will give 0
Let's assume 'variable a' and 'variable b'. Here, 'a' contains 20 and 'b' contains 10.
SQL Comparison Operators
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
20
Operator Description Example= It checks if two operands values are equal or not. (a=b) is not true
!= It checks if two operands values are equal or not (a!=b) is true
<> It checks if two operands values are equal or not (a<>b) is true
> It checks if the left operand value is greater than right operand value, (a>b) is not true
< It checks if the left operand value is less than right operand value. (a<b) is true
>=It checks if the left operand value is greater than or equal to the rightoperand value.
(a>=b) is not true
<= It checks if the left value is less than or equal to the right value. (a<=b) is true
!< It checks if the left value is not less than the right operand value. (a!<b) is not true
!> It checks if the left value is not greater than the right value. (a!>b) is true
SQL Logical Operators
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
21
Operator DescriptionALL It compares a value to all values in another value set.
Some It compares a value to some values in another value set.AND It allows the existence of multiple conditions in an SQL statement.ANY It compares the values in the list according to the condition.
BETWEEN It is used to search for values that are within a set of values.IN It compares a value to that specified list value.
NOT It reverses the meaning of any logical operator.OR It combines multiple conditions in SQL statements.
EXISTS It is used to search for the presence of a row in a specified table.LIKE It compares a value to similar values using wildcard operator.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUEThe UNIQUE operator searches every row of a specified table foruniqueness
SQL commands are instructions.
It is used to communicate with the database. It is also used to perform
specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users.
Structured Query Language(SQL) as we all know is the database language by
the use of which we can perform certain operations on the existing database
and also we can use this language to create a database. SQL uses certain
commands like Create, Drop, Insert etc. to carry out the required tasks.
SQL COMMAND
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
22
Types of SQL Command
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
23
DDL or Data Definition Language actually consists of the SQL commands that can be
used to define the database schema.
It simply deals with descriptions of the database schema and is used to create and
modify the structure of database objects in database.
1. CREATE – is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);
DDL(Data Definition Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
24
2. DROP – is used to delete objects from the database.
Syntax DROP TABLE ;
Example DROP TABLE EMPLOYEE;
3. TRUNCATE–is used to remove all records from a table, including all spaces
allocated for the records are removed, it does not disturb the structure of
table, i.e after truncate your table with all attributes will be exist but all the
rows in table will be deleted.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE EMPLOYEE;
DDL(Data Definition Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
25
4. COMMENT –is used to add comments to the data dictionary. This is similar to
comment we add in program block.
/* add comment here*/
6. RENAME –is used to rename an objects like database, tables, views etc.
existing in the database.
Syntax: Rename Object old_name to new_name;
Example: Rename table emp to employee;
7. ALTER-It is used to alter the structure of the table. This change could be
either to modify the characteristics of an existing attribute or probably to add a
new attribute or to remove the existing attributes.
DDL(Data Definition Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
26
1. ALTER TABLE command to add a New Column in an existing table :
Syntax: ALTER TABLE table_name ADD (column_namedatatype);
2. ALTER TABLE command to DROP COLUMN in an existing table:
Syntax: ALTER TABLE table_name DROP COLUMN column_name;
3. ALTER TABLE command to RENAME ATTRIBUTE in an existing table:
Syntax: ALTER TABLE table_name Rename old-column_name to new-column-
name;
4. ALTER TABLE command to change the DATA TYPE of a column in a table:
Syntax: ALTER TABLE table_name MODIFY (column_namedatatype);
DDL(Data Definition Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
27
DDL(Data Definition Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
28
5. ALTER TABLE command to add a NOT NULL constraint to a column in a table:
Syntax: ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
6. ALTER TABLE to ADD UNIQUE CONSTRAINT to a table:
Syntax: ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint
UNIQUE(column1, column2...);
7. ALTER TABLE command to ADD CHECK CONSTRAINT to a table:
Syntax: ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint
CHECK (CONDITION);
8. ALTER TABLE command to ADD PRIMARY KEY constraint to a table:
Syntax ALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey
PRIMARY KEY (column1, column2...);
9. ALTER TABLE command to DROP CONSTRAINT from a table:
Syntax: ALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint;
DDL(Data Definition Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
29
The SQL commands that deals with the manipulation of data present in
database belong to DML or Data Manipulation Language .
1. SELECT – isData Manipulation Language command used to retrieve data from
the tables of selected database. It retrieves the data and displays the retrieved
data on console.
Syntax: SELECT expressions FROM TABLES WHERE conditions;
For example: SELECT emp_name FROM employee WHERE age > 20;
DML(Data Manipulation Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
30
DML(Data Manipulation Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
31
2. INSERT – is Data Manipulation Language command used to insert data into
a table. It can insert a single record at a time in a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, …... col N) VALUES (value1, value2, …..
valueN);
Or INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");
DML(Data Manipulation Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
32
3. UPDATE – is DML command used to update existing data within a table. It
can modify the any values or attributes for the particular tuple based on
specified condition.
Syntax:
UPDATE tablename SET [column_name1= value1,...column_nameN = valueN]
[WHERE CONDITION]
Example: UPDATE students SET UserName = 'Sonu' WHERE StudentId = '3'
4. DELETE – is DML command used to delete records from a database table. It
deletes the one or more record from tables depends on a conditions.
Syntax: DELETE FROM table_name [WHERE condition];
Example: DELETE FROM JBook WHERE Author="Sonoo";
DCL includes commands such as GRANT and REVOKE which mainly deals with
the rights, permissions and other controls of the database system.
1. GRANT-gives user’s access privileges to database. It is also used to create a
database user with some privileges.
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
2. REVOKE-withdraw user’s access privileges given by using the GRANT
command.
Example:
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
DCL(Data Control Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
33
TCL(transaction Control Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
34
TCL commands deals with the transaction within the database.
1. COMMIT– commits a Transaction, is used to save all the transactions to the
database.
Example: DELETE FROM CUSTOMERS WHERE AGE = 25;
COMMIT;
2. ROLLBACK– rollbacks a transaction in case of any error occurs.Rollback
command is used to undo transactions that have not already been saved to the
database. It can rollback all transaction which are not yet committed.
Example: DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;
3. SAVEPOINT–sets a savepoint within a transaction.
4. SET TRANSACTION–specify characteristics for the transaction.
TCL(transaction Control Language)
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
35
VIEWS IN SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
36
Views in SQL are a virtual table. A view also contains rows and columns.
They have their own existence in database But they don’t save any records in
a view.
To create the view, we can select the fields from one or more tables present in
the database.
A view can either have specific rows based on certain condition or all the
rows of a table.
we can add SQL functions, WHERE, and JOIN statements to a view and
present the data as if the data were coming from one single table.
To understand views consider the following example and relations.
Table: Student_Detail Table: Student_Marks
VIEWS IN SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
37
STU_ID NAME ADDRESS1 Stephan Delhi2 Kathrin Noida3 David Ghaziabad4 Alina Gurugram
STU_ID NAME MARKS AGE1 Stephan 97 192 Kathrin 86 213 David 74 184 Alina 90 205 John 96 18
Creating view: A view can be created using the CREATE VIEW statement. We
can create a view from a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS SELECT column1, column2.....FROM table_name
WHERE condition;
2. Creating View from a single table: In this example, we create a View named
DetailsView from the table Student_Detail.
CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM Student_Details
WHERE STU_ID < 4;
Just like table query, we can query the view to view the data:
VIEWS IN SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
38
VIEWS IN SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
39
SELECT * FROM DetailsView;
The Output of this is as follow
3. Creating View from multiple tables: View from multiple tables can be
created by simply include multiple tables in the SELECT statement.
Example, a view is created named MarksView from two tables Student_Detail
and Student_Marks.
NAME ADDRESSStephan DelhiKathrin NoidaDavid Ghaziabad
VIEWS IN SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
40
CREATE VIEW MarksView AS SELECT Student_Detail.NAME, Student_Detail.
ADDRESS, Student_Marks.MARKS FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;
To display data of View MarksView:
SELECT * FROM MarksView;
Output of this will be as follow:
NAME ADDRESS MARKSStephan Delhi 97Kathrin Noida 86David Ghaziabad 74Alina Gurugram 90
VIEWS IN SQL
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
41
4. Deleting View: A view can be deleted using the Drop View statement.
Syntax:
DROP VIEW view_name;
Example: To delete the View MarksView, we can do this as:
DROP VIEW MarksView;
A Sub query or Inner query or a Nested query is a query within another SQL
query and embedded within the WHERE clause.
A sub query is used to return data that will be used in the main query as a
condition to further restrict the data to be retrieved.
Sub queries can be used with the SELECT, INSERT, UPDATE, and DELETE
statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
sub queries can retrieve the data from single or multiple tables.
NESTED QUERY
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
42
Rules for Writing Nested Query
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
43
1. Sub queries must be enclosed within parentheses.2. A sub query can have only one column in the SELECT clause, unless multiple
columns are in the main query for the sub query to compare its selectedcolumns.
3. An ORDER BY command cannot be used in a sub query, although the mainquery can use an ORDER BY. The GROUP BY command can be used toperform the same function as the ORDER BY in a sub query.
4. Sub queries that return more than one row can only be used with multiplevalue operators such as the IN operator.
5. The SELECT list cannot include any references to values that evaluate to aBLOB, ARRAY, CLOB, or NCLOB.
6. A sub query cannot be immediately enclosed in a set function.7. The BETWEEN operator cannot be used with a sub query. However, the
BETWEEN operator can be used within the sub query.
Nested Query
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
44
Example: Consider the CUSTOMERS table having the following records
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
Nested Query with the SELECT Statement
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
45
Nested Query are most frequently used with the SELECT statement.
Syntax: SELECT column_name [, column_name ]FROM table1 [, table2 ] WHERE
column_name OPERATOR (SELECT column_name [, column_name] FROM table1 [,
table2 ] [WHERE])
Now, let us check the following subquery with a SELECT statement.
SELECT *FROM CUSTOMERS WHERE ID IN (SELECT ID FROM CUSTOMERS
WHERE SALARY >4500);
The output of this Query
Will look like:
ID NAME AGE ADDRESS
SALARY
4 Chaitali 25 Mumbai 6500.005 Hardik 27 Bhopal 8500.007 Muffy 24 Indore 10000.00
Nested Query With The INSERT Statement
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
46
The INSERT statement uses the data returned from the subquery to insert
into another table.
The selected data in the sub query can be modified with any of the character,
date or number functions. The basic syntax is as follows.
INSERT INTO table_name [(column1 [,column2])] Operator (SELECT [
*|column1 [, column2 ] FROM table1 [, table2 ] [ WHERE VALUE OPERATOR ])
Example: Consider a table CUSTOMERS_BKP with similar structure as
CUSTOMERS table. Now to copy the complete CUSTOMERS table into the
CUSTOMERS_BKP table, you can use the following syntax.
INSERT INTO CUSTOMERS_BKP SELECT * FROM CUSTOMERS WHERE ID IN
(SELECT ID FROM CUSTOMERS) ;
Nested Query with the UPDATE Statement
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
47
The subquery can be used in conjunction with the UPDATE statement.
Either single or multiple columns in a table can be updated when using a
subquery with the UPDATE statement. The basic syntax is as follows.
UPDATE tableSET column_name = new_value[ WHERE OPERATOR [ VALUE
](SELECT COLUMN_NAME FROM TABLE_NAME [ WHERE) ] )
Example: Assuming, we have CUSTOMERS_BKP table available which is
backup of CUSTOMERS table. The following example updates SALARY by 0.25
times in the CUSTOMERS table for all the customers whose AGE is greater than
or equal to 27.
UPDATE CUSTOMERS SET SALARY = SALARY *0.25 WHERE AGE IN (SELECT
AGE FROM CUSTOMERS_BKP WHERE AGE >=27);
This would impact two rows and finally CUSTOMERS table would have the
following records
Nested Query with the UPDATE Statement
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
48
ID NAME AGE ADDRESS SALARY1 Ramesh 35 Ahmedabad 2500.002 Khilan 25 Delhi 1500.003 kaushik 23 Kota 2000.004 Chaitali 25 Mumbai 6500.005 Hardik 27 Bhopal 8500.006 Komal 22 MP 4500.007 Muffy 24 Indore 10000.00
The subquery can be used in conjunction with the DELETE statement like
with any other statements mentioned above. The basic syntax is as follows.
DELETE FROM TABLE_NAME[ WHERE OPERATOR [ VALUE ] (SELECT
COLUMN_NAME FROM TABLE_NAME [ WHERE) ])
Example: Assuming, we have a CUSTOMERS_BKP table available which is a
backup of the CUSTOMERS table. The following example deletes the records
from the CUSTOMERS table for all the customers whose AGE is greater than or
equal to 27.
DELETE FROM CUSTOMERS WHERE AGE IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >=27);
Nested Query with the DELETE Statement
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
49
This would impact two rows and finally the CUSTOMERS table would have the
following records.
Nested Query with the DELETE Statement
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
50
ID NAME AGE ADDRESS SALARY2 Khilan 25 Delhi 1500.003 kaushik 23 Kota 2000.004 Chaitali 25 Mumbai 6500.005 Hardik 27 Bhopal 8500.006 Komal 22 MP 4500.007 Muffy 24 Indore 10000.00
Indexes are special lookup tables. It is used to retrieve data from the
database very fast.
An Index is used to speed up select queries and where clauses.
it slow down the data input with insert and update statements.
Indexes can be created or dropped without affecting the data.
An index in a database is just like an index in the back of a book.
For example: When you reference all pages in a book that discusses a certain
topic, you first have to refer to the index, which alphabetically lists all the topics
and then referred to one or more specific page numbers.. The main reason is to
speed up querying so that you can get rows or sort rows faster.
SQL INDEX
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
51
SQL INDEX
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
52
First we need to understand how query without indexing runs. It basically traverse
each rows one by one & when it finds the data it returns. Refer the following image.
So suppose query is to find 50 , it will have to read 49 records as a linear
search. Refer the following image.
SQL INDEX
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
53
When we apply indexing, the query will quickly find out the data without
reading each one of them just by eliminating half of the data in each
traversal like a binary search.
The MySQL indexes are stored as B-tree where all the data are in leaf node.
Indexes are used to retrieve data from the database very fast.
The users cannot see the indexes, they are just used to speed up
searches/queries.
SQL INDEX
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
54
Types of Index55
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Clustered Index: A clustered index defines the order in which data is physically
stored in a table.
Table data can be sorted in only way, therefore, there can be only one clustered
index per table.
InSQL Server, the primary key constraint automatically creates a clustered
index on that particular column.
Ex. CREATE TABLE student(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, gen
der VARCHAR(50) NOT NULL,DOB datetime NOT NULL,total_score INT NOT NULL, ci
ty VARCHAR(50) NOT NULL).
in the “student” table we have set primary key constraint on the “id” colum
n. This automatically creates a clustered index on the “id” column.
Types of Index56
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Non-Clustered Indexes: A non-clustered index doesn’t sort the physical
data inside the table.
In fact, a non-clustered index is stored at one place and table data is stored
in another place.
This is similar to a textbook where the book content is located in one place
and the index is located in another.
This allows for more than one non-clustered index per table.
It is important to mention here that inside the table the data will be sorted
by a clustered index.
However, inside the non-clustered index data is stored in the specified
order.
Types of Index57
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
The index contains column values on which the index is created and the address of
the record that the column value belongs to.
When a query is issued against a column on which the index is created, the
database will first go to the index and look for the address of the corresponding row
in the table.
It will then go to that row address and fetch other column values. It is due to this
additional step that non-clustered indexes are slower than clustered indexes.
CREATE NONCLUSTERED INDEX IX_tblStudent_Name ON student(name ASC)
The above script creates a non-clustered index on the “name” column of the
student table. The index sorts by name in ascending order.
SQL INDEX58
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
1. Create Index statement
Syntax: CREATE INDEX index_name ON table_name (column1, column2, ...);
Example: CREATE INDEX idx_nameON Persons (LastName, FirstName);
2. Unique Index statement: It is used to create a unique index on a table. It
does not allow duplicate value.
Syntax:
CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);
Example: CREATE UNIQUE INDEX websites_idx ON websites (site_name);
3. Drop Index Statement: It is used to delete an index in a table.
Syntax: DROP INDEX index_name;
Example: DROP INDEX websites_idx;
SQL CLAUSES59
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Clauses are used to apply some condition or constraints on the query The
following are the various SQL clauses:
1. Where Clause
2. Order By Clause
3. Group By Clause
4. Having Clause
5. Distinct Clause
MySQL WHERE Clause60
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
SQL WHERE Clause is used with SELECT, INSERT, UPDATE and DELETE
clause to filter the results. It specifies a specific position where you have to
do the operation.
Syntax: WHERE conditions;
Conditions: It specifies the conditions that must be fulfilled for records to be
selected.
SELECT * FROM officers WHERE address = 'Mau';
MySQL WHERE Clause with AND condition: In this example, we are
retrieving data from the table "officers" with AND condition.
SELECT * FROM officers WHERE address = 'Lucknow' AND officer_id < 5;
ORDER BY CLAUSE61
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
The ORDER BY clause sorts the result-set in ascending or descending order.
It sorts the records in ascending order by default.
Syntax:SELECT column1, column2 FROM table_name WHERE condition ORD
ER BY column1, column2... ASC|DESC;
Where ASC: It is used to sort the result set in ascending order by
expression.
DESC: It sorts the result set in descending order by expression.
Example: Sorting Results in Ascending Order
SELECT * FROM CUSTOMER ORDER BY NAME;
Example: Sorting Results in Descending Order
SELECT * FROM CUSTOMER ORDER BY NAME DESC;
GROUP BY Clause62
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
The MYSQL GROUP BY Clause is used to collect data from multiple records and groupthe result by one or more column. It is generally used in a SELECT statement.SQL GROUP BY statement is used to arrange identical data into groups.You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. onthe grouped column.Syntax:SELECT expression1, expression2,... expression_n, aggregate_function (expression) FROM tables [WHERE conditions] GROUP BY expression1,...... expression_n;Parameters: expression1, expression2, ... expression_n: It specifies the expressionsthat are not encapsulated within an aggregate function and must be included in theGROUP BY clause.Aggregate function: It specifies function such as SUM, COUNT, MIN, MAX, AVG etc.WHERE conditions: It is optional. It specifies the conditions that must be fulfilled forthe records to be selected.Syntax:SELECT column FROM table_name WHERE conditions GROUP BY column ORDER BYcolumn
GROUP BY Clause63
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Example 1:SELECT COUNT(CustomerID), CountryFROM CustomersGROUP BY Country;
Example 2:SELECT COUNT(CustomerID), CountryFROM CustomersGROUP BY CountryORDER BY COUNT(CustomerID) DESC;
MySQL HAVING Clause64
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
MySQL HAVING Clause is used with GROUP BY clause. It always returns the rows where
condition is TRUE.
Syntax:SELECT expression1, expression2, ... expression_n, aggregate_function (expressio
n) FROM tables [WHERE conditions] GROUP BY expression1…..... expression_n HAVING
condition;
Parameters
1. Aggregate function: It specifies aggregate function Like SUM, COUNT, MIN, MAX, AVG.
2. expression1,… ... expression_n: It specifies the expressions that are not encapsulated
within an aggregate function and must be included in the GROUP BY clause.
3. HAVING condition: It is used to restrict the groups of returned rows. It shows only
those groups in result set whose conditions are TRUE.
MySQL HAVING Clause65
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Execute the following query:
SELECT emp_name, SUM(working_hours) AS "Total working hours" FROM empl
oyees GROUP BY emp_name HAVING SUM(working_hours) > 5;
MySQL Distinct Clause66
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
MySQL DISTINCT clause is used to remove duplicate records from the tableand fetch only the unique records.The DISTINCT clause is only used with the SELECT statement.Syntax:SELECT DISTINCT expressions FROM tables [WHERE conditions];Parameters1. expressions: specify the columns or calculations that you want to retrieve.2. tables: specify the name of the tables from where you retrieve records.
There must be at least one table listed in the FROM clause.3. WHERE conditions: It is optional. It specifies the conditions that must be
met for the records to be selected.
Use the following query: SELECT DISTINCT address FROM officers;
SET OPERATIONS67
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
SQL supports few Set operations which can be performed on the table data.
These are used to get meaningful results from data stored in the table,
under different special conditions.
Or The SQL Set operation is used to combine the two or more SQL SELECT
statements. Four different types of SET operations, are
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
SET OPERATIONS68
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
To Understand the concept let us consider the example with two relations
as follow.
Table: First Table: Second
Now let us see each set operation with this example.
ID Name
1 Abhi
2 Adam
ID Name
2 adam
3 Chester
UNION OPERATIONS69
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
UNION is used to combine the results of two or more SELECT statements.
However it will eliminate duplicate rows from its resultset.
In case of union, number of columns and data type must be same in both the
tables, on which UNION operation is being applied.
Select * from First Union Select * from Second
The resultset table will look like
ID NAME1 abhi2 Adam3 Chester
UNION ALL OPERATIONS70
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
This operation is similar to Union. But it also shows the duplicate rows
Select * from First Union All Select * from Second
The resultset table will look like
ID NAME1 abhi2 Adam2 Adam3 Chester
INTERSECTOPERATIONS71
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Intersect operation is used to combine two SELECT statements, but it only
returns the records which are common from both SELECT statements.
In case of Intersect the number of columns and data type must be same.
But remember MySQL does not support INTERSECT operator.
Select * from First INTERSECT Select * from SecondThe resultset table will look like
ID NAME2 Adam
MINUS OPERATIONS
72
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
The Minus operation combines results of two SELECT statements and return
only those in the final result, which belongs to the first set of the result.
Select * from First MINUS Select * from SecondThe resultset table will look like
ID NAME2 Adam
SET MEMBERSHIP73
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
To Check whether any values is a member of some set or not set membership
is used.
In SQL set membership is used with IN and NOT IN operator which allows
you to easily test if an expression matches any value in a list of values.
The IN operator checks a value within a set of values separated by commas
and retrieve the rows from the table which are matching.
The IN returns 1 when the search value present within the range otherwise
returns 0.Consider Example with Relations
1. SavingAccount(Laccno, SCust_Name, Balance)
2. Loan Account (Laccno, LCust_Name, Balance)
SET MEMBERSHIP74
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
1. Find list of all customer name which are having Loan account as wellsaving account
Select * from SavingAccount where SCust_Name IN (Select * fromLoanAccount)
2. Find list of Customer Belonging to any ne of following city.
SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK');
SET COMPARISON75
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Set comparison is SQL is used to compare the values in two set.
Using arithmetic comparison operator we can just compare two values but if
we want to compare more than one elements of two sets then set comparison
can be used with SOME and ALL operator.
First we will understand how All and Some operator work.
Remember SOME operator will match any element of Set A with SOME
elements of Set B.
and ALL Operator will match any element of Set A with ALL elements of Set B.
Consider two Sets
X={100, 200, 300} And Y = {300, 400, 500}
SET COMPARISON76
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Condition for Some Operator
Result for Some Operator
Condition for All Operator
Result for All Operator
A < SOME B TRUE A < ALL B TRUEA <= SOME B TRUE A <= ALL B TRUEA >= SOME B TRUE A >= ALL B FALSEA > SOME B FALSE A > ALL B FALSE
A <> SOME B TRUE A <> ALL B FALSEA = SOME B TRUE A = ALL B FALSE
1. Find the name of all branches that have assets greater than those of somebranches located in nashik.
Select bname from branch where assets > SOME (Select assets from Branch wherebcity=’Nashik’)
2. Find the name of all branches that have assets greater than those of ALLbranches located in nashik.
Select bname from branch where assets > ALL (Select assets from Branch wherebcity=’Nashik’)
SQL JOINS77
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
The SQL Joins clause is used to combine records from two or more tables in a
database.
A JOIN is a means for combining fields from two tables by using values
common to each.
A SQL Join statement is used to combine data or rows from two or more
tables based on a common field between them.
Let us Discus Different types of Joins with two tables Order & Customer
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
SQL JOINS78
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
customer_id first_name last_name address city1 George Washington 3200 Mt Vernon Hwy Mount Vernon2 John Adams 1250 Hancock St Quincy
3 Thomas Jefferson 931 Thomas Jefferson Pkwy Charlottesville
4 James Madison 11350 Constitution Hwy Orange
5 James Monroe 2050 James Monroe Parkway Charlottesville
order_id order_date amount customer_id1 07/04/1776 $234.56 12 03/14/1760 $78.50 33 05/23/1784 $124.00 24 09/03/1790 $65.50 35 07/21/1795 $25.50 10
6 11/27/1787 $14.40 9Table 2: Order
Table 1: Customer
INNER JOIN79
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
The INNER JOIN keyword selects all rows from both the tables as long as the
condition satisfies.
This keyword will create the result-set by combining all rows from both the
tables where the condition satisfies, i.e value of the common field will be
same.
Syntax: Select t1.attribute, t2.attribute from Table1 t1 INNER JOIN Table t2 ON
t1.matching_Column= t2.matching_Column
Example:
Select c.first_name, c.last_name,o. order_date, o.order_amount from customers cinner join orders o onc.customer_id=o.customer_id
INNER JOIN80
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Output of inner join will be as follow
first_name last_name order_date order_amountGeorge Washington 07/4/1776 $234.56
John Adams 05/23/1784 $124.00Thomas Jefferson 03/14/1760 $78.50Thomas Jefferson 09/03/1790 $65.50
LEFT JOIN81
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
It is also called as Left Outer Join. This join returns all the rows of the table
on the left side of the join and matching rows for the table on the right side
of join.
The rows for which there is no matching row on right side, the result-set
will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax: Select t1.attribute, t2.attribute from Table1 t1 LEFT JOIN Table t2
ON t1.matching_Column= t2.matching_Column
Example:
Select c.first_name, c.last_name,o. order_date, o.order_amount from customers
cLeft join orders o onc.customer_id=o.customer_id
LEFT JOIN82
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Output of Left join will be as follow
first_name last_name order_date order_amountGeorge Washington 07/04/1776 $234.56
John Adams 05/23/1784 $124.00Thomas Jefferson 03/14/1760 $78.50Thomas Jefferson 09/03/1790 $65.50
James Madison NULL NULLJames Monroe NULL NULL
RIGHT JOIN83
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
it is also called as Right Outer Join. RIGHT JOIN is similar to LEFT JOIN.
This join returns all the rows of the table on the right side of the join and
matching rows for the table on the left side of join.
The rows for which there is no matching row on left side, the result-set will
contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN.
Syntax: Select t1.attribute, t2.attribute from Table1 t1 RIGHT JOIN Table t2
ON t1.matching_Column= t2.matching_Column
Example:
Select c.first_name, c.last_name,o. order_date, o.order_amount from customers
c Right join orders o onc.customer_id=o.customer_id
RIGHT JOIN84
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Output of Right join will be as follow
first_name last_name order_date order_amountGeorge Washington 07/04/1776 $234.56Thomas Jefferson 03/14/1760 $78.50
John Adams 05/23/1784 $124.00Thomas Jefferson 09/03/1790 $65.50
NULL NULL 07/21/1795 $25.50NULL NULL 11/27/1787 $14.40
FULL JOIN85
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
FULL JOIN creates the result-set by combining result of both LEFT JOIN and
RIGHT JOIN.
The result-set will contain all the rows from both the tables.
The rows for which there is no matching, the result-set will
contain NULL values.
Syntax: Select t1.attribute, t2.attribute from Table1 t1 FULL JOIN Table t2
ON t1.matching_Column= t2.matching_Column
Example:
Select c.first_name, c.last_name,o. order_date, o.order_amount from customers
c Full join orders oOnc.customer_id=o.customer_id
FULL JOIN86
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Output of Full join will be as followfirst_name last_name order_date order_amount
George Washington 07/04/1776 $234.56Thomas Jefferson 03/14/1760 $78.50
John Adams 05/23/1784 $124.00Thomas Jefferson 09/03/1790 $65.50
NULL NULL 07/21/1795 $25.50NULL NULL 11/27/1787 $14.40James Madison NULL NULLJames Monroe NULL NULL
AGGREGATE FUNCTIONS87
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
In database management an aggregate function is a function where the
values of multiple rows are grouped together as input on certain criteria to
form a single value of more significant meaning.
Different aggregate functions are as follow
AGGREGATE FUNCTIONS88
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
1. COUNT Function: is used to Count the number of rows in a database table
Example: SELECT COUNT(*) FROM PRODUCT_MAST;
2. SUM Function: is used to calculate the sum of all selected columns.
Example: SELECT SUM(COST) FROM PRODUCT_MAST;
3. AVG function: is used to calculate the average value of the numeric type.
Example: SELECT AVG(COST)FROM PRODUCT_MAST;
4. MAX Function: is used to find the maximum value of a certain column.
Example: SELECT MAX(RATE) FROM PRODUCT_MAST;
5. MIN Function: is used to find the minimum value of a certain column.
Example: SELECT MIN(RATE) FROM PRODUCT_MAST;
TUPLE VARIABLE89
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
A tuple variable is variable that takes on tuples of a particular relation
schema as values. That is, every value assigned to a given tuple variable has
the same number and type of fields. Consider the Table
Select Order_date, Sum(Amount) AS ‘Sale_of_Day ’ from order group by
order_date. order_id order_date amount1 07/04/1776 $234.562 07/04/1776 $78.503 05/23/1784 $124.004 05/23/1784 $65.505 07/21/1795 $25.506 11/27/1787 $14.40
order_date Sale_of_Day07/04/1776 $313.0605/23/1784 $189.5007/21/1795 $25.5011/27/1787 $14.40
The Output of this will look like this where as Sale_of_Day is a tuple variable.
ORDERING OF TUPLES90
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Ordering of tuple is the process of ordering the rows in a table in some
order i.e. either ascending or descending order.
This can be used with Order by clause. To arrange the rows in ascending
order we use the symbol ASC and for arranging the rows in descending
order we use DESC symbol along with order by clause.
If we does not specify the order sequence then by default it will arrange the
rows in ascending order.
Example: Select * from Order ORDER BY order_id ASC; OR
Select * from Order ORDER BY order_id DESC;
INTRODUCTION TO MONGODB91
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
MongoDB was developed by Eliot Horowitz and Dwight Merriman in the
year 2007
MongoDB is a NoSQL database which stores the data in form of key-value
pairs.
It is an Open Source, Document Database which provides high performance
and scalability along with data modelling and data management of huge sets
of data in an enterprise application.
MongoDB also provides the feature of Auto-Scaling. Since, MongoDB is a
cross platform database and can be installed across different platforms like
Windows, Linux etc.
INTRODUCTION TO MONGODB92
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
What is Document based storage?
A Document is nothing but a data structure with name-value pairs like in
JSON.
It is very easy to map any custom Object of any programming language with
a MongoDB Document.
For example : Studentobject has attributes name, rollno and subject where
subjects is a List
{ Name: ’ABCD’
Rollno: 1
Subject: [C, C++, JAVA, PYTHON]
}
INTRODUCTION TO MONGODB93
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
What is NoSQL:
NoSQL, is basically a database used to manage huge sets of unstructured
data, where in the data is not stored in tabular relations like relational
databases.
Most of the currently existing Relational Databases have failed in solving
some of the complex modern problems.
Difference between Joins & Set Operations94
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL
Joins Set Operations
Joins can be used to combine datafrom two or more tables
UNIONS can be used to combine datafrom two or more tables
joins combine data into newcolumns.
Unions combine data into newrows.
If two tables are joined together, thenthe data from the first table is shownin one set of column alongside thesecond table’s column in the samerow.
If two tables are “unioned” together,then the data from the first table is inone set of rows, and the data from thesecond table in another set. The rowsare in the same result.
Joins are faster than union (Setoperations)
Comparatively slower than Joins
Thank You95
Prepared By: Prof. V. K. Wani Unit No. 3: Introduction to SQL