2. SYLLABUS
• Query language: Relational Algebra
• Characteristics and various operations
• SQL
• Characteristics of SQL
• SQL data types and literals
• Concept of Data definition language
3. Cont….
• Data manipulation language
• Data Control language: Basic Structure
• Set operations
• Aggregate Functions
• Null Values
• Views
• Nested Sub-queries
4. Query language: Relational Algebra
• Relational algebra refers to a procedural query language that takes relation
instances as input and returns relation instances as output.
• It performs queries with the help of operators.
• A binary or unary operator can be used.
• They take in relations as input and produce relations as output.
• Recursive relational algebra is applied to a relationship, and intermediate
outcomes are also considered relations.
5. The following are the fundamental operations
present in a relational algebra:
• Select Operation
• Project Operation
• Union Operation
• Set Different Operation
• Cartesian Product Operation
• Rename Operation
6. Select Operation (or σ)
• It selects tuples from a relation that satisfy the provided predicate.
• The notation is − σp(r)
• Here σ stands for the selection predicate while r stands for the relation. p refers to
the prepositional logic formula that may use connectors such as or, and, and not.
Also, these terms may make use of relational operators such as − =, ≠, ≥, < , >, ≤.
• Example
• σsubject = “information”(Novels)
• The output would be − Selecting tuples from the novels wherever the subject
happens to be ‘information’.
7. Project Operation (or ∏)
• It projects those column(s) that satisfy any given predicate.
• Here B1, B2 , An refer to the attribute names of the relation r.
• The notation is − ∏B1, B2, Bn (r)
• Remember that duplicate rows are eliminated automatically, since relation is a set.
• Example
• ∏subject, writer (Novels)
• The output would be − Selecting and projecting columns named as writer as well
as the subject from the relation Novels.
8. Union Operation (or ∪)
• It would perform binary union between two relations.
• The notation is − r U s
• It is defined as follows:
• r ∪ s = { t | t ∈ r or t ∈ s}
• Here r and s either refer to DB relations or the relation result set (or temporary
relation).
• The given conditions must hold if we want any union operation to be valid:
• s, and r must contain a similar number of attributes.
9. CONT…..
• The domains of an attribute must be compatible.
• The duplicate tuples are eliminated automatically.
• ∏ writer (Novels) ∪ ∏ writer (Articles)
• The output would be − Projecting the names of those writers who might
have written either an article or a novel or both.
10. Set Different Operation (or −)
• Tuples refers to the result of the set difference operation. These are present
in just one of the relations but not at all in the second one.
• The notation is − r − s
• Finding all the tuples present in r and not present in s.
• ∏ writer (Novels) − ∏ writer (Articles)
• The output would be − Providing the writer names who might have
written novels but have not written articles.
11. Cartesian Product Operation (or Χ)
• It helps in combining data and info of two differing relations into a single one.
• The notation is − r Χ s
• Where s and r refer to the relations. Their outputs would be defined as the follows:
• s Χ r = { t ∈ s and q t | q ∈ r}
• σwriter = ‘mahesh'(Novels Χ Articles)
• The output would be − Yielding a relation that shows all the articles and novels
written by mahesh.
12. Rename Operation (or ρ)
• Relations are the results of the relational algebra, but without any name.
Thus, the rename operation would allow us to rename the relation output.
The ‘rename’ operation is basically denoted by the small Greek
letter ρ or rho.
• The notation is − ρ x (E)
• Here the result of the E expression is saved with the name of x.
14. Characteristics and various operations
• The relational algebra is a theoretical procedural query language which takes an instance of
relations and does operations that work on one or more relations to describe another
relation without altering the original relation(s). Thus, both the operands and the outputs are
relations. So the output from one operation can turn into the input to another operation,
which allows expressions to be nested in the relational algebra, just as you nest arithmetic
operations. This property is called closure: relations are closed under the algebra, just as
numbers are closed under arithmetic operations.
• The relational algebra is a relation-at-a-time (or set) language where all tuples are controlled
in one statement without the use of a loop. There are several variations of syntax for
relational algebra commands, and you use a common symbolic notation for the commands
and present it informally.
15. SQL
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL became a standard of the American National Standards Institute
(ANSI) in 1986, and of the International Organization for Standardization
(ISO) in 1987
16. What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
17. Characteristics of SQL
• The ANSI SQL provides with:
• Specific syntax and semantics of SQL data definition and data manipulation languages.
• It also provides with basic data structure and operations for designing, assessing, maintaining, controlling and
protecting SQL databases.
• Portability of database definition and application is also provided. Applications can be moved from one
machine to another.
• IS professionals share a common language and reduce training costs.
• Professionals can become proficient in its use and increase the productivity.
• It provides with longevity.
• It provides with reduced dependence on single vendor.
18. Rules to write commands:
• Table names cannot exceed 20 characters.
• Name of the table must be unique.
• Field names also must be unique.
• The field list and filed length must be enclosed in parentheses.
• The user must specify the field length and type.
• The field definitions must be separated with commas.
• SQL statements must end with a semicolon.
19. SQL data types and literals
• SQL data types define the type of value that can be stored in a table column. For example,
if you want a column to store only integer values, you can define its data type as INT.
• SQL data types can be broadly divided into the following categories.
• Numeric data types such as: INT, TINYINT, BIGINT, FLOAT, REAL, etc.
• Date and Time data types such as: DATE, TIME, DATETIME, etc.
• Character and String data types such as: CHAR, VARCHAR, TEXT, etc.
• Unicode character string data types such as: NCHAR, NVARCHAR, NTEXT, etc.
• Binary data types such as: BINARY, VARBINARY, etc.
• Miscellaneous data types - CLOB, BLOB, XML, CURSOR, TABLE, etc.
20. Concept of Data definition language
• A data definition language (DDL) is a computer language used to create and modify the
structure of database objects in a database.
• These database objects include views, schemas, tables, indexes, etc.
• The present database industry incorporates DDL into any formal language describing data.
• However, it is considered to be a subset of SQL (Structured Query Language).
• SQL often uses imperative verbs with normal English such as sentences to implement
database modifications.
• Hence, DDL does not show up as a different language in an SQL database, but does define
changes in the database schema.
21. CONT…..
• It is used to establish and modify the structure of objects in a database by
dealing with descriptions of the database schema. Unlike data manipulation
language (DML) commands that are used for data modification purposes,
DDL commands are used for altering the database structure such as creating
new tables or objects along with all their attributes (data type, table name,
etc.).
• Commonly used DDL in SQL querying are CREATE, ALTER, DROP, and
TRUNCATE.
22. Data manipulation language
• A DML (data manipulation language) refers to a computer programming
language that allows you to add (insert), delete (delete), and alter (update)
data in a database.
• A DML is typically a sublanguage of a larger database language like SQL,
with the DML containing some of the language’s operators. A DML (data
manipulation language) is a group of computer languages that provide
commands for manipulating data in databases.
23.
24. Data Control language: Basic Structure
• DCL includes commands such as GRANT and REVOKE which mainly deal with
the rights, permissions, and other controls of the database system.
• List of DCL commands:
• GRANT: This command gives users access privileges to the database.
• Syntax:
• GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER,
ANOTHER_USER;
25. Cont…..
• REVOKE: This command withdraws the user’s access privileges given by
using the GRANT command.
• Syntax:REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1,
USER2;
26. Set operations
• Set operators in MySQL allow you to manage and compare data sets. Data from one or
more tables or queries can be combined, intersected, or subtracted using them.
• The three most frequently utilized set operators are UNION, INTERSECT, and EXCEPT.
• A single result set is created by combining the output of two or more SELECT operations
using UNION.
• INTERSECT returns the rows shared by the result sets of two SELECT statements.
• The rows that are specific to the first SELECT statement and absent from the
second SELECT statement are returned by EXCEPT.
27. UNION Operator
• The UNION operator is one of the most often-used set operators in MySQL. It
combines the results of two or more SELECT operations into a single result set.
• The syntax for using the UNION operator is as follows:
• SELECT column1, column2, ...
• FROM table1
• UNION
• SELECT column1, column2, ...
• FROM table2
29. Union All
• Union All operation is equal to the Union operation.
• It returns the set without removing duplication and sorting the data.
• SELECT column_name FROM table1
• UNION ALL
• SELECT column_name FROM table2;
31. 3. Intersect
• It is used to combine two SELECT statements. The Intersect operation
returns the common rows from both the SELECT statements.
• In the Intersect operation, the number of datatype and columns must be the
same.
• It has no duplicates and it arranges the data in ascending order by default.
33. Minus
• It combines the result of two SELECT statements.
• Minus operator is used to display the rows which are present in the first
query but absent in the second query.
• It has no duplicates and data arranged in ascending order by default.
• SELECT column_name FROM table1
• MINUS
• SELECT column_name FROM table2;
35. Aggregate Functions
• 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.
• Various Aggregate Functions
• 1) Count()
• 2) Sum()
• 3) Avg()
• 4) Min()
• 5) Max()
36. Now let us understand each Aggregate function
with a example:
• Id Name Salary
• -----------------------
• 1 A 80
• 2 B 40
• 3 C 60
• 4 D 70
• 5 E 60
• 6 F Null
37. Count():
• Count(*): Returns total number of records .i.e 6.
Count(salary): Return number of Non Null values over the column salary.
i.e 5.
Count(Distinct Salary): Return number of distinct Non Null values over
the column salary .i.e 4
• Sum():
• sum(salary): Sum all Non Null values of Column salary i.e., 310
• sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.
38. Cont….
• Avg():
•
• Avg(salary) = Sum(salary) / count(salary) = 310/5
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 250/4
•
• Min():
•
• Min(salary): Minimum value in the salary column except NULL i.e., 40.
Max(salary): Maximum value in the salary i.e., 80.
39. Null Values
• A field with a NULL value is a field with no value.
• If a field in a table is optional, it is possible to insert a new record or update
a record without adding a value to this field.
• Then, the field will be saved with a NULL value.
• SELECT column_names
FROM table_name
WHERE column_name IS NULL;
40. Views
• In SQL, a view is a virtual table based on the result-set of an SQL statement.
• A view contains rows and columns, just like a real table. The fields in a view
are fields from one or more real tables in the database.
• You can add SQL statements and functions to a view and present the data as
if the data were coming from one single table.
• A view is created with the CREATE VIEW statement.
41. Cont….
• CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
• CREATE VIEW [Brazil Customers] AS
• SELECT CustomerName, ContactName
• FROM Customers
• WHERE Country = 'Brazil';
42. Nested Sub-queries
• A Subquery or Inner query or a Nested query is a query within another SQL
query and embedded within clauses, most commonly in the WHERE clause.
It is used to return data from a table, and this data will be used in the main
query as a condition to further restrict the data to be retrieved.
• Subqueries can be used with the SELECT, INSERT, UPDATE, and
DELETE statements along with the operators like =, <, >, >=, <=, IN,
BETWEEN, etc.
43. There are a few rules that subqueries must
follow −
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause, unless multiple
columns are in the main query for the subquery to compare its selected columns.
• An ORDER BY command cannot be used in a subquery, although the main query
can use an ORDER BY. The GROUP BY command can be used to perform the
same function as the ORDER BY in a subquery.
• Subqueries that return more than one row can only be used with multiple value
operators such as the IN operator.
44. Cont…
• The SELECT list cannot include any references to values that evaluate to a
BLOB, ARRAY, CLOB, or NCLOB.
• A subquery cannot be immediately enclosed in a set function.
• The BETWEEN operator cannot be used with a subquery. However, the
BETWEEN operator can be used within the subquery.
45. Subqueries with the SELECT Statement
• SELECT column_name [, column_name ]
• FROM table1 [, table2 ]
• WHERE column_name OPERATOR
• (SELECT column_name [, column_name ]
• FROM table1 [, table2 ]
• [WHERE])
46. Cont…
• SELECT *
• FROM CUSTOMERS
• WHERE ID IN (SELECT ID
• FROM CUSTOMERS
• WHERE SALARY > 4500) ;
47. Subqueries with the INSERT Statement
• INSERT INTO table_name [ (column1 [, column2 ]) ]
• SELECT [ *|column1 [, column2 ]
• FROM table1 [, table2 ]
• [ WHERE VALUE OPERATOR ]
48. Cont…..
• INSERT INTO CUSTOMERS_BKP
• SELECT * FROM CUSTOMERS
• WHERE ID IN (SELECT ID
• FROM CUSTOMERS) ;
49. Subqueries with the UPDATE Statement
• UPDATE CUSTOMERS
• SET SALARY = SALARY * 0.25
• WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
• WHERE AGE >= 27 );
50. Subqueries with the DELETE Statement
• DELETE FROM CUSTOMERS
• WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
• WHERE AGE >= 27 );