SQL1
SQL1
Back End
• It is the knowledge base behind the front end
• It comprises three components: server, application, and database.
IMPORTANT
Q1.What is the difference between SQL and MYSQL? (Asked by INFOSYS, TCS in 2017,18,19)
Q2.What is the full form of PHP? (Asked by INFOSYS in 2019)
WHAT IS THE ROLE OF “SQL” IN ANY PROJECT
WHAT IS THE ROLE OF “SQL” IN ANY PROJECT
IMPORTANT
Q1.Full form of SQL? (Asked by INFOSYS, TCS in 2017,18,19)
Q2.Why did we say that SQL is a universal language? (Asked by INFOSYS in 2019)
Introduction
• SQL stands for Structured Query Language
• SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F in 1970
• SQL is used to communicate with a database
• SQL is a non-procedural language
• IN SQL we need to only describe what we ‘want to be done’.
• Also, they are using different dialects, such as −
• MS SQL Server using T-SQL,
• Oracle using PL/SQL,
• MS Access version of SQL is called JET SQL (native format) etc.
IMPORTANT
Q1.What is the difference between DDL and DML ? (Asked by INFOSYS, TCS in 2017,18,19)
Q2.What is schema? (Asked by MIND in 2019)
SQL Environment
• Schema
• The structure that contains descriptions of objects created by a user (base tables, views, constraints)
TRUNCATE DELETE
DROP MERGE
RENAME
COMMENT
SQL COMMANDS
DROP DELETE
TRUNCATE MERGE
RENAME
DDL COMMANDS…
• The Create Command to create table
SYNTAX
Create table TableName(clo1 datatype(size), clo2 datatype(size)[CONSTRAINT],……. clon
datatype(size));
EXAMPLE
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
TRUNCATE DELETE
DROP MERGE
RENAME
COMMENT
Writing Basic
SQL SELECT Statements
Capabilities of SQL SELECT Statements
Projection Selection
Table 1 Table 1
Join
Table 1 Table 2
Basic SELECT Statement
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Using Arithmetic Operators
SELECT last_name, salary, salary + 300
FROM employees;
…
Operator Precedence
_
* / +
…
Using Parentheses
SELECT last_name, salary, 12*(salary+100)
FROM employees;
…
Defining a Null Value
• A null is a value that is unavailable, unassigned, unknown, or
inapplicable.
• A null is not the same as zero or a blank space.
SELECT last_name, job_id, salary, commission_pct
FROM employees;
…
Null Values
in Arithmetic Expressions
Arithmetic expressions containing a null value
evaluate to null.
SELECT last_name, 12*salary*commission_pct
FROM employees;
…
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name - there can also be the
optional AS keyword between the column name and alias
• Requires double quotation marks if it contains spaces or
special characters or is case sensitive
Using Column Aliases
SELECT last_name AS name, commission_pct comm
FROM employees;
…
Concatenation Operator
A concatenation operator:
• Concatenates columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character expression
Using the Concatenation Operator
SELECT last_name||job_id AS "Employees"
FROM employees;
…
Literal Character Strings
• A literal is a character, a number, or a date included in the
SELECT list.
• Date and character literal values must be enclosed within
single quotation marks.
• Each character string is output once for each
row returned.
Using Literal Character Strings
…
Duplicate Rows
The default display of queries is all rows, including
duplicate rows.
SELECT department_id
FROM employees;
…
Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT
keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;
Limiting Rows Using a Selection
EMPLOYEES
“retrieve all
employees
in department 90”
Limiting the Rows Selected
• Restrict the rows returned by using the WHERE clause.
= Equal to
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
Using the LIKE Condition
• You can combine pattern-matching characters.
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
• You can use the ESCAPE identifier to search for the actual % and _
symbols.
Using the NULL Conditions
Test for nulls with the IS NULL operator.
…
Sorting by Column Alias
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal;
…
SQL COMMANDS
TRUNCATE DELETE
DROP MERGE
RENAME
COMMENT
Data Manipulation Language
• A DML statement is executed when you:
• Add new rows to a table
• Modify existing rows in a table
• Remove existing rows from a table
• A transaction consists of a collection of DML statements
that form a logical unit of work.
The INSERT Statement Syntax
• Add new rows to a table by using the INSERT statement.
• All rows in the table are modified if you omit the WHERE clause.
UPDATE copy_emp
SET department_id = 110;
22 rows updated.
Removing a Row from a Table
DEPARTMENTS
• All rows in the table are deleted if you omit the WHERE
clause.
TRUNCATE DELETE
DROP MERGE
RENAME
COMMENT
Controlling User Access
Database
administrator
Users
Privileges
• Database security:
• System security
• Data security
• System privileges: Gaining access to the database
• Object privileges: Manipulating the content of the
database objects
• Schemas: Collections of objects, such as tables, views,
and sequences
Creating Users
The DBA creates users by using the CREATE USER
statement.
Manager
Privileges
GRANT select
ON alice.departments
TO PUBLIC;
Grant succeeded.
How to Revoke Object Privileges
• You use the REVOKE statement to revoke privileges
granted to other users.
• Privileges granted to others through the WITH GRANT
OPTION clause are also revoked.