Department of Computer Science and Engineering: Certification Course
Department of Computer Science and Engineering: Certification Course
CERTIFICATION COURSE
ON
COURSE MATERIAL
STRUCTURED QUERY LANGUAGE
What is SQL?
Introduction to databases:
Examples:
• Telephone directory
• Customer data
• Product inventory
• Visitors’ register
• Weather records
A database uses a table to store data in a structured way. A table is basically a collection of
information in the form of data entries and contains rows and columns to store data.
For example, in the above student table, the field consists of id, name, age and department.
A row of a table is also called a record. It specifies some information about each individual
entry in the table.
It represents horizontally in the table.
A column represents vertically in the table which contains information about the header which is
the column name.
Age
23
21
23
22
NULL values:
The NULL value in the table means the field with “space”. It is different if it is filled as ‘0’ or if it
contains space.
Data Integrity:
· Entity integrity: This specifies that there should be no duplicate rows in a table.
· Domain Integrity: This specifies that there should be valid entries for a given column
according to the type, the range of values and format etc.
· Referential Integrity: This specifies that rows cannot be deleted which are used by other
rows/records in the table.
· Use defined Integrity: This specifies some business rules that are defined by users.
DBMS Operations:
Introduction to RDBMS:
• A relational database refers to a database that stores data in a structured format, using rows and
columns.
• It is “relational” because the values within each table are related to each other. Tables may also
be related to other tables.
• The relational structure makes it possible to run queries across multiple tables at once.
Features of RDBMS:
DBMS is basically meant to deal with small RDBMS is meant to handle large amounts of
data. data.
• The key difference is that RDBMS (relational database management system) applications store
data in a tabular form, whereas in traditional approach, applications store data as files.
• There can be, but there will be no “relation” between the tables, like in a RDBMS. In traditional
approach, data is generally stored in either a hierarchical form/navigational form. This means that
a single data unit will have 0, 1 or more children nodes and 1 parent node.
SQL statement:
of the SQL commands/keywords like SELECT, INSERT, DELETE, DROP, UPDATE, ALTER
Example explanation:
Semicolon is used to separate SQL statements. It is the standard way of practice to separate SQL
statements in a database system in which more than one SQL statements are used in the same call.
SQL Syntax:
SQL Select:
SQL Where:
From table_name
Where condition;
SQL Process:
The system determines the best way to convey out our request and SQL engine figures out how to
interpret the task.
Isn’t it a smarter idea to know how the internal flow of a process is?
I am curious to make you think that way and let us see the various components included in the
process.
SQL Commands:
Normalization:
Functional Dependency:
• Consider the relation
• Student# and course# together defines exactly one value of marks. Student#, course# ,Marks
• Student# and course# determines Marks or Marks is functionally dependent on student# and
course#
• Course# – CourseName
Normalization: Types
Example:
Student Marks Table in 1NF
Student table:
Result table:
101 M1 82 A
102 P4 83 A
103 B3 68 B
Course table:
3 Normal Form:
No transitive dependency exists between non-key attributes and key attributes through another
non-key attribute.
Result_table:
101 M1 82 A
102 P4 83 A
103 B3 68 B
Marks → Grade
Result table:
101 M1 82
102 P4 83
103 B3 68
Marks Grade
82 A
83 A
68 B
Advantages:
Disadvantages:
Why SQL?
1. Numeric – bit, tinyint, smallint, int, bigint, decimal, numeric, float, real
2. Character/String – Char, Varchar, Varchar(max), Text
3. Date/Time – Date, Time, Datetime, Timestamp, Year
4. Unicode character/String -Nchar, Nvarchar, Nvarchar(max), NText
5. Binary- Binary, Varbinary, Varbinary(max), image
6. Miscellaneous- Clob, Blob, Json, XML
SQL Constraints:
Constraint Description
Index Creates and retrieves data from the database very quickly.
Syntax:
Syntax:
NOTE: If you delete or drop the database, all the tables and views will be deleted. So be careful
while using this command. Also there is a slight difference between drop and delete command.
You will find it in further lessons.
Syntax:
Here, we need to select a database first before executing any query on table/database.
Syntax:
SQL Keys:
A column/columns is called the primary key that uniquely identifies each row in the table.
When in case of create/alter table we can define primary key constraint if we want to create the
primary key. If there are multiple columns used as a primary key, then it is called a composite
primary key.
Syntax:
Primary key(ID));
NOTE: When we use ALTER TABLE statement to add a primary key, the primary key column
must NOT contain NULL values(when the table was initially created)
In Relational databases, a foreign key is a field or a column that is used to develop a link between
two tables.
To simply say, a foreign key in one table used to point to the primary key in another table.
First table:
Second table:
Faculty_ID Faculty_Name Student_id
Here you see that “Student_id” in the second table points to the “Student_id” in the first table.
The “Student_id” column in the first table is the PRIMARY key in the first table.
The “Student_id” column in the second table is a FOREIGN key in the second table.
Primary key(Student_id),
Below are some important difference between primary key and foreign key in SQL-
Syntax:
col2 int,
col3 varchar(50),
A Unique key is a set of one or more than one fields/columns of a table that uniquely identifies a
record in a database/table.
The Unique key and primary key both provide uniqueness for a column or set of columns.
If a table has more than one candidate key, one of them will be primary and others will be alternate
keys.
Basically, an alternate key is just a candidate key that has not been selected as the primary key.
SQL Table:
CREATE table
Syntax:
DROP table:
Syntax:
Ex:
Used to delete rows from a table. If specific row is to be deleted, then we use WHERE condition
to filter the rows in the table
Syntax:
-DELETE from table_name;
Syntax:
ADD –
MODIFY:
.
.
DROP:
Syntax:
REAME to new_table_name;
Subsets of SQL:
• DCL (Data Control Language) : assignment and removal of permissions• TCL (Transaction
Control Language) : saving and restoring changes to a database
Command Description
first_name VARCHAR(10),
ALTER command:
Rename command:
Truncate Command:
Drop Command:
Command Description
Update command:
UPDATE employees
SET last_name=‘Cohen’
WHERE emp_id=101;
Delete command:
Command Description
Savepoint Identify a point in a transaction to which you can roll back later
SQL Operators:
SQL statements generally contain some reserved word or a character in a ‘where’ clause to perform
operations such as comparison and logical operations.
Operator Description
Subtraction(-) Its subtracts right hand operand from left hand operand
If you are wondering right hand operand and left hand operand, the see below illustration:
X+Y
X*Y
Operator Description
In SQL’s where condition, it compares a value to all values with other values.TRUE if all the
AND
conditions separated by AND is TRUE
In SQL’s where condition, it compares a value to all values with other values.TRUE if all the
OR
conditions separated by OR is TRUE
The LIKE operator is used to compare a value to similar values using wildcard
LIKE
operators.Checks an attribute value matches a given string pattern
BETWEEN Checks an attribute value within range. Given the maximum value and minimum value.
TRUE if the operand is equal to one of a list of expressions.Checks an attribute value matches
IN
any value within a value list
TRUE
NOT NOT(5<2)
Sample Queries:
Operator Description
= Equal to
Sample Queries:
Function Description
Sample Queries:
WHERE Clause :
• Used to specify a condition while fetching the data from a single table or by joining with multiple
tables.
• Not only used in the SELECT statement, but it is also used in the UPDATE, DELETE statement,
etc.,
e.g.
The example mentioned above extracts all the columns from the table ‘employees’ whose
emp_id=101
GROUP BY clause:
• This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause if used.
e.g.,
SELECT SUM(salary),dept_id
FROM employees
GROUP BY dept_id
HAVING CLAUSE
• Used with aggregate functions due to its non-performance in the WHERE clause.
• Must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if
used.
e.g.,
SELECT AVG(salary),dept_id
FROM employees
GROUP BY dept_id
HAVING count(dept_id)>=2
ORDER BY CLAUSE
• Can Sort in Reverse (Descending) Order with “DESC” after the column name
e.g.,
• Used to combine the results of two SELECT statements including duplicate rows.
• The same rules that apply to the UNION clause will apply to the UNION ALL operator.
SYNTAX:
UNION ALL
UNION ALL
• Used to combine the results of two SELECT statements including duplicate rows.
• The same rules that apply to the UNION clause will apply to the UNION ALL operator.
SYNTAX:
UNION ALL
SQL UNION
• Used to combine the result-set of two or more SELECT statements removing duplicates
• Each SELECT statement within the UNION must have the same number of columns
• The selected columns must be of similar data types and must be in the same order in each
SELECT statement
• More than two queries can be clubbed using more than one UNION statement
SQL JOINS
Combine rows/columns from one or more tables, based on a related column between them in a
database
• LEFT JOIN − Returns all rows from the left table, even if there are no matches in the right table.
• RIGHT JOIN − Returns all rows from the right table, even if there are no matches in the left
table.
• FULL OUTER JOIN − Returns rows when there is a match in one of the tables.
• SELF JOIN − Used to join a table to itself as if the table were two tables, temporarily renaming
at least one table in the SQL statement.
• CARTESIAN JOIN (CROSS JOIN) − Returns the Cartesian product of the sets of records from
the two or more joined tables.
The INNER JOIN creates a new result table by combining column values of two tables (table1 and
table2) based upon the join-predicate. The query compares each row of table1 with each row of
table2 to find all pairs of rows which satisfy the join-predicate.
SYNTAX :
FROM table1
INNER JOIN table2
ON table1.commonfield = table2.commonfield;
The LEFT JOIN returns all the values from the left table, plus matched values from the right table
or NULL in case of no matching join predicate.
SYNTAX :
FROM table1
ON table1.commonfield = table2.commonfield;
• Department names will be NULL for those employees in which there are no departments
• The RIGHT JOIN returns all the values from the right table, plus matched values from the left
table or NULL in case of no matching join predicate.
SYNTAX :
ON table1.commonfield = table2.commonfield;
• For those departments where there are no managers, their names will be shown as NULL
The FULL OUTER JOIN combines the results of both left and right outer joins. The joined table
will contain all records from both the tables and fill in NULLs for missing matches on either side.
SYNTAX :
FROM table1
ON table1.commonfield = table2.commonfield;
Union
FROM table1
ON table1.commonfield = table2.commonfield;
SYNTAX:
• The CROSS JOIN produces a result set with the number of rows in the first table multiplied by
the number of rows in the second.
SYNTAX:
FROM table1
To help you master SQL perfectly, GL academy has launched free upskilling courses.