SQL Notes
SQL Notes
RD & SQL
Monday, May 06, 2024 5:28 PM
Database
• Organized collection of data about an entity or things
• It can be integrated as well as shared
Advantages:
1. Elimination of data redundancy.(duplication of data)
2. Sharing of data
3. Reduced programming effort
4. Data consistency
5. Database enforces standards
6. Improved data integrity
7. Privacy and security
8. Improved backup and recovery system
Types
1.Hierarchical method
Records are organized as trees. It represents a hierarchy of parent and
child segments.
Represents one-to-many(1:M) relationships b/w parent and child
segment.
https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 1/5
06/05/2024, 21:17 OneNote
Advantages
• Can handle more relationship types
• Data access is easy and more flexible
• it includes DDL data definition language as well as DML data
manipulation language
• it commodes better data integrity
Limitations
• Quite complicated system several numbers of links
• structural changes require changes to be made in all application
programs
• Extra memory is consumed for holding links
• High level language interface is required to interact with the database
Database keys
Primary key-set of one or more attributes/fields which uniquely identifies
a tuple/row in a table.
Candidate key-it refers to all the attributes in a relation that are candidates
or capable of becoming a primary key.
candidate key-primary key=alternate key
Alternate key-a candidate key that is not the primary key is called an
alternate key.
Foreign key-it is a non-key attribute whose value is derived from the
primary key of another table.
DDL Commands
1. Creating database
Create database<database_name>;
2. Opening database
USE database<database_name>;
https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 2/5
06/05/2024, 21:17 OneNote
3. Removing database
Drop database<database_name>;
4. Create table
Create table<table_name>;
5. Viewing table
Show tables;
6. Viewing table structure
DESCRIBE <table_name>;
7. Alter table structure
Used for
-adding a column
-renaming existing column
-changing datatype and modifying size of column
-to remove or physically delete a column
(a)adding a column to an existing table
ALTER TABLE<table_name>ADD<column_name><data type>
[size]/default data;
(b)Modifying an existing column defination
Alter table<table_name>
Modify([column_name]<data type>);
(c)Renaming a column
Alter table<table_name>
Change<old-column-name><new-column-name>column_defination;
(e)Removing a column
Alter table<table_name>Drop<column name>
(f)Adding and deleting primary key constraints
Alter table employee add/drop primary key(Emp_ID);
8.Drop table command
Drop table<table_name>;
DML command
1.The insert into command is used to insert a new record/row/tuple in a
table.
(a)Inserting data(for all columns)into a table:
Insert into<table_name>values(value1,value2,value3...)
(b)Inserting data by specifying all the column names and associated values
into a table:
Insert into<table_name>(column1,column2,columnN,…)
Values(value1,value2,valueN,...);
(c)Inserting data into specific columns of a table:
Insert into<table_name>[(column1,column2,...columnN)]
Values[(value1,value2,...valueN)]
2.Modify data into table
Update<table_name>
SET<column1>=<value1>,<column2>=<value2>,…
Where<condition>;
3.Removing data from a table
Delete from<table_name>where<condition>;
-SQL Truncate statement(used to delete all the rows from the table and
free the space containing the table for reuse)
Truncate table<table_name>;
SQL SELECT Statement
1.Projection
Select <column-name1>,<column-name2> from <Table-name>;
2.Selection-WHERE clause
Select*From <table_name> where<conditions_to_satistfy>;
3.Reordering columns while displaying query result
Ex. Select Name,Roll no,DOB,Marks From Student;
4.Eliminating Duplicate/Redundant Data-DISTINCT clause
Select distinct<column-name> from <Table-name>;
SQL operators
Logical functions
https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 3/5
06/05/2024, 21:17 OneNote
1.AND
2.OR
3.NOT
4.SPECIAL
a.Between...AND
Select<colounm name> from <tablename> where <columnname>
BETWEEN<value1>AND<value2>;
b.Conditions based on a List-IN
Select<colounm name> from <tablename> where <columnname> IN/NOT
IN(value1,value2,...);
c.Conditions based on Pattern-LIKE
Select<colounm name> from <tablename> where <columnname>
LIKE/NOT LIKE <pattern>;
%-matches any sting
_-matches any one char
SORTING in SQL-ORDER BY
It is used to sort the data in ascending and descending orderbased on one
or more columns.
For descending [DESC]
Select<column-list>FROM<Table-name>[WHERE<condition>] ORDER BY
<column_name>[ASC or DESC]
Where clause optional
Ex. Select rollno,name,marks from student ORDER BY Name;
Select rollno,name,marks from student ORDER BY marks DESC,
Name;
AGGREGATE FUNCTIONS
1.MAX()
2MIN()
3.SUM()
4.AVG()
5.COUNT()
GROUP BY
Select<colunm1,column2,column_n<aggregate_function(expression)>
FROM <tables>WHERE<conditions>GROUP BY<colunm1,column2,…
column_n>;
HAVING CLAUSE
Select<colunm1,column2,column_n<aggregate_function(expression)>
FROM <tables>WHERE<conditions>GROUP BY<colunm1,column2,…
column_n>HAVING[<condition1...condition_n>];
Having is only used with group by clause
SQL JOINS
Equi join
SELECT <column_list> FROM <table1, table2....> WHERE <table1.primary
key column> = <table2.foreign key column>;
https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 4/5