SQL Notes
SQL Notes
Introduction
to SQL
• SQL stands for Structured Query
Language
3 4
05/06/2024
and is used to create and modify the structure of database objects in the database. purpose of the DQL Command is to get some schema relation based on the query passed to
DDL is a set of SQL commands used to create, modify, and delete database structures it. We can define DQL as follows it is a component of SQL statement that allows getting data
but not data. These commands are normally not used by a general user, who should be from the database and imposing order upon it. It includes the SELECT statement. This
accessing the database via an application. command allows getting the data out of the database to perform operations with it. When a
• List of DDL commands: SELECT is fired against a table or tables the result is compiled into a further temporary table,
• CREATE: This command is used to create the database or its objects (like which is displayed or perhaps received by the program i.e. a front-end.
table, index, function, views, store procedure, and triggers).
• DROP: This command is used to delete objects from the database. • List of DQL:
• ALTER: This is used to alter the structure of the database.
• SELECT: It is used to retrieve data from the database.
5 6
7 8
05/06/2024
SQL
CREATE The SQL CREATE
DATABASE Statement
Syntax
ANSWER:
The SQL column2 datatype,
CREATE TABLE column3 datatype,
CREATE DATABASE SAMPLE;
Statement ....
);
TINYTEXT Holds a string with a maximum length of 255 characters INTEGER(size) Equal to INT(size)
TEXT(size) Holds a string with a maximum length of 65,535 bytes BIGINT(size) A large integer. Signed range is from -9223372036854775808 to 9223372036854775807. Unsigned range is from 0 to
18446744073709551615. The size parameter specifies the maximum display width (which is 255)
BLOB(size) For BLOBs (Binary Large Objects). Holds up to 65,535 bytes of data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters FLOAT(size, d) A floating point number. The total number of digits is specified in size. The number of digits after the decimal point is
specified in the d parameter. This syntax is deprecated in MySQL 8.0.17, and it will be removed in future MySQL versions
MEDIUMBLOB For BLOBs (Binary Large Objects). Holds up to 16,777,215 bytes of data
FLOAT(p) A floating point number. MySQL uses the p value to determine whether to use FLOAT or DOUBLE for the resulting data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
type. If p is from 0 to 24, the data type becomes FLOAT(). If p is from 25 to 53, the data type becomes DOUBLE()
LONGBLOB For BLOBs (Binary Large Objects). Holds up to 4,294,967,295 bytes of data
DOUBLE(size, d) A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal
ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 point is specified in the d parameter
values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are DOUBLE PRECISION(size, d)
sorted in the order you enter them DECIMAL(size, d) An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is
specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value
SET(val1, val2, val3, ...) A string object that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values for size is 10. The default value for d is 0.
13 14
in a SET list DEC(size, d) Equal to DECIMAL(size,d)
19 20
05/06/2024
INSERT
ANSWER:
SQL
DROP TABLE STUDENT;
UPDATE
DELETE
21 22
Statement 2. If you are adding values for all the columns of the table, OUTPUT: OID ONAME CONTACT
you do not need to specify the column names in the SQL OW1 Kamal 0775658568
query. However, make sure the order of the values is in the OW2 Mala 0775658511
same order as the columns in the table. Here, the INSERT OW3 Raja 0776658786
INTO syntax would be as follows: OW4 Nimesh 0775325548
VALUES VALUES ("OW8", "Rajesh", "0775686599"); SQL UPDATE SET column1 = value1, column2 = value2, ...
WHERE condition;
Statement
OUTPUT: OID ONAME CONTACT Note: Be careful when updating records in a table!
OW1 Kamal 0775658568
Notice the WHERE clause in the UPDATE statement.
OW2 Mala 0775658511
OW3 Raja 0776658786 The WHERE clause specifies which record(s) that
OW4 Nimesh 0775325548
should be updated. If you omit the WHERE clause, all
OW5 Raja 0771112336
OW6 Raahul 0715588568 records in the table will be updated!
OW7 Nimal 0775688133
25 26
OW8 Rajesh 0775686599
ANSWER:
DELETE Syntax
UPDATE OWNER
SET ONAME = "KAMELESH" DELETE FROM table_name
ANSWER:
31 32
05/06/2024
2. Retrieve all the details from EMP Table. 3. Retrieve EMPID, EMPNAME, DIPID from EMP Table.
OUTPUT: OUTPUT:
EMPID EMPNAME CONTACTNO DIPID EMPID EMPNAME DIPID
33 34
35 36
05/06/2024
WORK *
/
Multiply
Divide
<
>=
Less than
Greater than or equal to O
TOGETHER % Modulo <= Less than or equal to
P
WITH SQL
<> Not equal to
SQL LOGICAL OPERATORS E
SELECT
Operator Description
SQL BITWISE OPERATORS R
A
AND TRUE if all the conditions separated by AND is TRUE
The SQL
WHERE Syntax:
ANSWER:
WHERE
SELECT column1, column2, ... OUTPUT:
FROM table_name EMPID EMPNAME
SELECT EMPID, EMPNAME
Clause WHERE condition;
FROM EMP
E001 Kamal
E005 Kamal
WHERE EMPNAME = “Kamal”;
Note: The WHERE clause is not only
used in SELECT statements, it is also
used in UPDATE, DELETE, etc.!
39 40
05/06/2024
OUTPUT: ANSWER:
ANSWER:
OUTPUT:
EMPID EMPNAME CONTACTNO DIPID
SELECT * SELECT DID, DNAME DID DNAME
E001 Kamal 0775658568 D01
D01 Accounts
FROM EMP E005 Kamal 0771112336 D02 FROM DEPARTMENT
WHERE EMPNAME = “Kamal”; WHERE DID = “D01”;
41 42
43 44
05/06/2024
9. Retrieve all the details of employee who get more 10. Retrieve all the details of employee who get more
than 1,000.00 allowance from salary Table. than 500.00 allowance from salary Table.
OUTPUT:
ANSWER: ANSWER:
OUTPUT: EMPID ALLOWANCE BASICSAL
SELECT * EMPID ALLOWANCE BASICSAL SELECT * E002 1,000.00 35,000.00
E006 2,000.00 55,000.00 E004 1,000.00 40,000.00
FROM SALARY FROM SALARY
E005 1,000.00 42,000.00
WHERE ALLOWANCE > 1000; WHERE ALLOWANCE > 500; E006 2,000.00 55,000.00
45 46
• The WHERE clause can be combined with AND, OR, 11. Retrieve all the details of employee who get more than
and NOT operators.
500.00 allowance and more than 40,000.00 basicsal from
salary Table.
• The AND and OR operators are used to filter records
based on more than one condition:
ANSWER:
The SQL • The AND operator displays a record if all the conditions
separated by AND are TRUE. SELECT *
AND, OR
and NOT • The OR operator displays a record if any of the
conditions separated by OR is TRUE.
FROM SALARY
ANSWER:
• The ORDER BY keyword sorts the records in
SELECT * The SQL ascending order by default. To sort the records in
Keyword
WHERE EMPNAME = “Kamal” AND DIPID = “D01”; ORDER BY Syntax
13. Retrieve all the details of employees in emp table, 14. Retrieve all the details of employees in emp table,
sorted by empname in ascending order sorted by empname in descending order
OUTPUT: OUTPUT:
EMPID EMPNAME CONTACTNO DIPID EMPID EMPNAME CONTACTNO DIPID
ANSWER: ANSWER:
E001 Kamal 0775658568 D01 E004 Rahu 0775325548 D03
SELECT * E005 Kamal 0771112336 D02 SELECT * E003 Nimesh 0776658786 D01
E006 Leela 0715588568 D01 E002 Mala 0775658511 D02
FROM EMP E002 Mala 0775658511 D02 FROM EMP E006 Leela 0715588568 D01
E003 Nimesh 0776658786 D01 E001 Kamal 0775658568 D01
ORDER BY EMPNAME ASC; ORDER BY EMPNAME DESC;
E004 Rahu 0775325548 D03 E005 Kamal 0771112336 D02
51 52
05/06/2024
ANSWER:
SELECT COUNT(EMPID) AS COUNTOFEMP, DIPID
OUTPUT:
COUNTOFEMP DIPID
The SQL HAVING Syntax
55
ORDER BY column_name(s); 56
05/06/2024
ANSWER:
OUTPUT:
SELECT COUNT(EMPID) AS COUNTOFEMP, DIPID
COUNTOFEMP DIPID
FROM EMP
2 D02
GROUP BY DIPID 3 D01
57 58
SQL JOIN
A JOIN clause is used to combine rows from two or
SQL INNER JOIN Keyword
more tables, based on a related column between
them. The INNER JOIN keyword selects records that have
Different Types of SQL JOINs matching values in both tables.
• (INNER) JOIN: Returns records that have
matching values in both tables
INNER JOIN Syntax
• LEFT (OUTER) JOIN: Returns all records from
the left table, and the matched records from SELECT column_name(s)
the right table FROM table1
• RIGHT (OUTER) JOIN: Returns all records from INNER JOIN table2
the right table, and the matched records from
ON table1.column_name = table2.column_name;
the left table
OWNER OW3
OW4
Raja
Nimesh
0776658786
0775325548
VEHICLE V003
V004
Van
Car
Blue
White
OW3
OW4
OW5 Raja 0771112336 V005 Three wheel Yellow OW5
OW6 Raahul 0715588568 V006 Van White OW6
61 62
19. Retrieve oid, oname, contact, vid, model, color, bid SQL LEFT JOIN Keyword
and bdate from owner, vehicle and booking tables.
ANSWER: The LEFT JOIN keyword returns all records from the left table
SELECT OID, ONAME, CONTACT, VEHICLE.VID, MODEL, COLOR, BID, BDATE (table1), and the matching records from the right table
FROM ((VEHICLE (table2). The result is 0 records from the right side, if there is no
20. Retrieve vid, model, color, bid and bdate from SQL RIGHT JOIN Keyword
vehicle and booking tables (use left join).
ANSWER: The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1).
SELECT VEHICLE.VID, MODEL, COLOR, BID, BDATE
The result is 0 records from the left side, if there is no match.
FROM VEHICLE LEFT JOIN BOOKING ON VEHICLE.VID = BOOKING.VID; RIGHT JOIN Syntax
SELECT VEHICLE.VID, MODEL, COLOR, BID, BDATE table is joined with itself.
OUTPUT:
VID MODEL COLOR BID BDATE
V001 Van White B01 12/01/2020
V001 Van White B03 15/02/2020
V002 Car Black B02 12/01/2020
V003 Van Blue B04 20/05/2020
71