Sql Basics
Sql Basics
Syntax:
REATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHA
R2(100), DOB DATE);
Data Definition Language (DDL)- Drop
Drop: It is used to delete both the structure and record
stored in the table.
Syntax:
DROP TABLE ;
Example:
DROP TABLE EMPLOYEE;
Data Definition Language (DDL)- ALTER
ALTER: It is used to alter the structure of the database. This change
could be either to modify the characteristics of an existing
attribute or probably to add a new attribute.
Syntax:
ALTER TABLE table_name ADD column_name COLUMN-definition;
ALTER TABLE MODIFY(COLUMN DEFINITION....);
Example:
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
Data Definition Language (DDL)- TRUNCATE
TRUNCATE: It is used to delete all the rows from the table and free
the space containing the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
Data Manipulation Language
• DML commands are used to modify the database. It is
responsible for all form of CHANGES in the database.
OR
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Example:
INSERT INTO XYZ (Author, Subject) VALUES ("Sonoo", "DBMS");
Data Manipulation Language - UPDATE
Update: This command is used to update or modify the value of a
column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_n
ameN = valueN] [WHERE CONDITION]
Example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Data Control Language
DCL commands are used to GRANT and TAKE BACK
authority from any database user.
Revoke
Data Control Language - Grant
GRANT: It is used to give user access privileges to a database.
Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOT
HER_USER;
Example:
Syntax:
SELECT expressions FROM TABLES WHERE conditions;
Example:
SELECT emp_name FROM employee WHERE age > 20;
Example:
SQL> CREATE TABLE EMPLOYEE (
EMP_ID INT NOT NULL,
EMP_NAME VARCHAR (25) NOT NULL,
PHONE_NO INT NOT NULL,
ADDRESS CHAR (30),
PRIMARY KEY (ID)
);
• DESC EMPLOYEE;
• DELETE FROM table_name WHERE condition
• DROP TABLE "table_name";
• SELECT * FROM table_name;
• INSERT INTO TABLE_NAME VALUES (value1, value2, value 3, .... Value N);
• INSERT INTO TABLE_NAME[(col1, col2, col3,.... col N)] VALUES (value1, value2, valu
e 3, .... Value N);
• UPDATE table_name SET column_name = value WHERE condition;
Example:
• UPDATE table_name SET column_name = value1, column_name2 = value
WHERE condition;
• DELETE FROM table_name WHERE some_condition;
SQL Sub Query
A Subquery is a query within another SQL query and embedded
within the WHERE clause.
Important Rule:
• A subquery can be placed in a number of SQL clauses like WHERE clause,
FROM clause, HAVING clause.
• You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
• A subquery is a query within another query. The outer query is known as
the main query, and the inner query is known as a subquery.
• Subqueries are on the right side of the comparison operator.
• A subquery is enclosed in parentheses.
• In the Subquery, ORDER BY command cannot be used. But GROUP BY
command can be used to perform the same function as ORDER BY
command.
Subqueries with the Select Statement
SQL subqueries are most frequently used with the Select statement.
Syntax:
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
Example:
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
Subqueries with the INSERT Statement
• SQL subquery can also be used with the Insert statement. In the insert
statement, data returned from the subquery is used to insert into another
table.
• In the subquery, the selected data can be modified with any of the
character, date functions.
Syntax:
INSERT INTO table_name (column1, column2, column3....)
SELECT * FROM table_name WHERE VALUE OPERATOR
Example:
INSERT INTO EMPLOYEE_BKP
SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE);
Subqueries with the UPDATE Statement
The subquery of SQL can be used in conjunction with the Update
statement. When a subquery is used with the Update statement,
then either single or multiple columns in a table can be updated.
Syntax:
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR
(SELECT COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example:
Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table. The given example updates the SALARY by .25 times in the
EMPLOYEE table for all employee whose AGE is greater than or equal to 29.
UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);
Subqueries with the DELETE Statement
The subquery of SQL can be used in conjunction with the Delete
statement just like any other statements mentioned above.
Syntax:
DELETE FROM TABLE_NAME WHERE VALUE OPERATOR
(SELECT COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example:
Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table. The given example deletes the records from the EMPLOYEE
table for all EMPLOYEE whose AGE is greater than or equal to 29.
Example
Syntax SELECT *
SELECT column1, column2
FROM table_name FROM CUSTOMER
WHERE condition ORDER BY NAME;
ORDER BY column1, column2... AS OR
C|DESC; SELECT *
FROM CUSTOMER
ORDER BY NAME DESC;
SQL Aggregate Functions
COUNT FUNCTION
• COUNT function is used to Count the number of rows in a database
table. It can work on both numeric and non-numeric data types.
• COUNT function uses the COUNT(*) that returns the count of all the
rows in a specified table. COUNT(*) considers duplicate and Null.
Syntax
COUNT(*) or COUNT( [ALL|DISTINCT] expression )
Example
SELECT COUNT(*) FROM PRODUCT_MAST;
SELECT COUNT(*) FROM PRODUCT_MAST; WHERE RATE>=20;
SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT_MAST;
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY;
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY
HAVING COUNT(*)>2;
SUM FUNCTION
• Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
Syntax
SUM() or SUM( [ALL|DISTINCT] expression )
Example
SELECT SUM(COST) FROM PRODUCT_MAST;
SUM() with WHERE
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3;
SUM() with GROUP BY
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3
GROUP BY COMPANY;
SUM() with HAVING
SELECT COMPANY, SUM(COST) FROM PRODUCT_MAST GROUP BY COM
PANY HAVING SUM(COST)>=170;
AVG FUNCTION
• The AVG function is used to calculate the average value of the
numeric type. AVG function returns the average of all non-Null
values.
Syntax
AVG() or AVG( [ALL|DISTINCT] expression )
Example
SELECT AVG(COST) FROM PRODUCT_MAST;
MAX FUNCTION
• MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
Syntax
MAX() or MAX( [ALL|DISTINCT] expression )
Example
SELECT MAX(RATE) FROM PRODUCT_MAST;
MIN FUNCTION
• MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column
Syntax
MIN() or MIN( [ALL|DISTINCT] expression )
Example
SELECT MIN(RATE) FROM PRODUCT_MAST;