Create: Create, Alter, Truncate, Drop Rename Grant Revoke Select, Insert, Delete Update

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

 DDL: The Data Definition Language is used to define the database and schema

structure by using a set of SQL Queries like CREATE, ALTER, TRUNCATE, DROP, and
RENAME.
 DCL: The Data Control Language is used to control user access to the database via a
set of commands like GRANT and REVOKE in the SQL Query.
 DML: The Data Manipulation Language is used for maintaining the data by using
SQL Queries like SELECT, INSERT, DELETE and UPDATE.

DDL is short name of Data Definition Language, which deals with database schemas and
descriptions, of how the data should reside in the database.

 CREATE - to create a database and its objects like (table, index, views, store
procedure, function, and triggers)
 ALTER - alters the structure of the existing database
 DROP - delete objects from the database
 TRUNCATE - remove all records from a table, including all spaces allocated for the
records are removed
 COMMENT - add comments to the data dictionary
 RENAME - rename an object

DML is short name of Data Manipulation Language which deals with data manipulation
and includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE,
etc., and it is used to store, modify, retrieve, delete and update data in a database.

 SELECT - retrieve data from a database


 INSERT - insert data into a table
 UPDATE - updates existing data within a table
 DELETE - Delete all records from a database table
 MERGE - UPSERT operation (insert or update)
 CALL - call a PL/SQL or Java subprogram
 EXPLAIN PLAN - interpretation of the data access path
 LOCK TABLE - concurrency Control

DCL is short name of Data Control Language which includes commands such as GRANT
and mostly concerned with rights, permissions and other controls of the database system.

 GRANT - allow users access privileges to the database


 REVOKE - withdraw users access privileges given by using the GRANT command

TCL is short name of Transaction Control Language which deals with a transaction within a
database.

 COMMIT - commits a Transaction


 ROLLBACK - rollback a transaction in case of any error occurs
 SAVEPOINT - to rollback the transaction making points within groups
 SET TRANSACTION - specify characteristics of the transaction

What is Index and how it can help performance?

Indexes are database objects designed to improve query performance. By applying indexes to
one or more columns in table or views, we could improve data retrieval time from these
tables. Index hunting helps in improving the speed as well as the query performance of the
database. This can be achieved via the following measures:

 The query optimizer is used to coordinate the study of queries with the workload and
the best use of queries is suggested based on this.
 Index and query distribution along with their performance are observed to check the
effect.
 Tuning databases to a small collection of problem queries is also recommended.

The IS NULL Operator

The IS NULL operator is used to test for empty values (NULL values).

The following SQL lists all customers with a NULL value in the "Address" field:

Example

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;
The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE
statement. The WHERE clause specifies which record(s) that should be updated. If you omit the
WHERE clause, all records in the table will be updated!

The DELETE statement is used to delete existing records in a table.

DELETE Syntax

DELETE FROM table_name WHERE condition;

Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!

The SQL GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(),
SUM(), AVG()) to group the result-set by one or more columns.

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country;

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
The SQL HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.

The following SQL statement lists the number of customers in each country. Only include
countries with more than 5 customers:

Example

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

The following SQL statement lists the number of customers in each country, sorted high to
low (Only include countries with more than 5 customers):

Example

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

The SQL SELECT INTO Statement

The SELECT INTO statement copies data from one table into a new table.

SELECT INTO Syntax

Copy all columns into a new table:

SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;

The SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another
table.
The INSERT INTO SELECT statement requires that the data types in source and target tables
matches.

Note: The existing records in the target table are unaffected.

INSERT INTO SELECT Syntax

Copy all columns from one table to another table:

INSERT INTO table2


SELECT * FROM table1
WHERE condition;

SQL CASE Examples

The following SQL goes through conditions and returns a value when the first condition is
met:

Example

SELECT OrderID, Quantity,


CASE
    WHEN Quantity > 30 THEN 'The quantity is greater than 30'
    WHEN Quantity = 30 THEN 'The quantity is 30'
    ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

The following SQL will order the customers by City. However, if City is NULL, then order
by Country:

Example

SELECT CustomerName, City, Country


FROM Customers
ORDER BY
(CASE
    WHEN City IS NULL THEN Country
    ELSE City
END);
Write CREATE TABLE statement to create the table ‘developers’ according
to the diagram

Diagram

Answer:

CREATE TABLE IF NOT EXISTS


`id` INT NOT NULL AUTO_INC
`first_name` VARCHAR(20) NO
`last_name` VARCHAR(25) NO

1 CREATE TABLE IF NOT EXISTS `developers` (

2   `id` INT NOT NULL AUTO_INCREMENT,

3   `first_name` VARCHAR(20) NOT NULL,

4   `last_name` VARCHAR(25) NOT NULL,

5   `hire_date` DATE NOT NULL,

6   `position` VARCHAR(10) NOT NULL DEFAULT 'backend',

7   `salary` DECIMAL(8,2) NULL DEFAULT NULL,

8   PRIMARY KEY (`id`),

9   INDEX `DEV_POS_IX` (`position` ASC),

10   INDEX `DEV_NAME_IX` (`last_name` ASC, `first_name` ASC))


11 ENGINE = InnoDB

12 DEFAULT CHARACTER SET = utf8

8. Write a query to get monthly salary of all developers, if the table contains
salaries for the year

SELECT first_name, last_name


FROM developers;

1 SELECT first_name, last_name, round(salary/12,2) as 'monthly salary'

2    FROM developers;

9. Write a query to get the maximum salary of a back-end developer

SELECT MAX(salary)
FROM developers
WHERE position='backend';

1 SELECT MAX(salary)

2 FROM developers

3 WHERE position='backend';

10. Write a query to find the 5th maximum salary in the developers table
SELECT DISTINCT salary
FROM developers d1
WHERE 5 = (SELECT COUNT(
FROM developers d2

1 SELECT DISTINCT salary

2 FROM developers d1

3 WHERE 5 = (SELECT COUNT(DISTINCT salary)

4 FROM developers d2

5 WHERE d2.salary >= d1.salary);

11. Write a query using JOIN to find the name (first_name, last_name) and
hire date of the developers who were hired after ‘Wilson’

MySQL
SELECT d1.first_name, d1.last_
FROM developers d1
JOIN developers d2
ON (d2.last_name = 'Wilson')

1 SELECT d1.first_name, d1.last_name, d1.hire_date

2 FROM developers d1

3 JOIN developers d2

4 ON (d2.last_name = 'Wilson')

5 WHERE d2.hire_date < d1.hire_date;


PRIMARY KEYS

Tables are created and then we need to add PK


FOREIGN KEYS

Tables are created and then we need to add FK

ON DELETE CASCADE

You might also like