0% found this document useful (0 votes)
5 views59 pages

SQL Mynotes

The document outlines the CBSE Computer Science syllabus for 2025-26, focusing on Structured Query Language (SQL) and its components. It covers SQL commands, data types in MySQL, constraints, and various commands for database management, including DDL and DML. Additionally, it explains aggregate functions, the HAVING clause, and different types of joins in SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views59 pages

SQL Mynotes

The document outlines the CBSE Computer Science syllabus for 2025-26, focusing on Structured Query Language (SQL) and its components. It covers SQL commands, data types in MySQL, constraints, and various commands for database management, including DDL and DML. Additionally, it explains aggregate functions, the HAVING clause, and different types of joins in SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

As Per CBSE Computer Science

Syllabus
2025-26

Chapter-7
What is a Structured Query Language (SQL)?

SQL is a standard language for storing, retrieving and manipulating data on a


relational database. All the relational database like MySql, Oracle, MS Access,
SQL server uses Structured query language(SQL) for accessing and
manipulating data.

SQL provides wide range of effective command to perform all sort of required
operations on data such as create tables, insert record, view recodes, update,
alter, delete, drop, etc.
What is DDL and DML?

All the SQL commands are categorized into five categories: DDL,DML,DCL,DQL,TCL. In this
course we are going to cover only DDL and DML commands in detail.

Data definition Language(DDL): Data Definition Language actually consists of the SQL
commands that can be used to define the database schema. It simply deals with
descriptions of the database schema and is used to create and modify the structure of
database objects in the database. Example: Create, Drop, Alter, Truncate.

Data Manipulation Language(DML): The SQL commands that deals with the manipulation of
data present in the database belong to DML or Data Manipulation Language and this
includes most of the SQL statements. Example: Insert, Delete, Update.
Data Types in MySQL

Data stored in a database table are of different types, As SQL developers we have chose the suitable data
types for each field while defining a table. SQL offers supports a wide range of data types from which the
developer can choose the most appropriate data types for each column.

char(size): used for fixed length string data.


Example: A column defined as char(10) , it can contain string values of maximum 10 length. SQL allocates 10
bytes of memory irrespective of legth of data to be stored.

varchar(size): used for variable length string data.


Example: If a column defined as varchar(10) , SQL allocates maximum 10 bytes to each value, but bytes
allocated may vary depending on the length of data.
int( ): Used for integer/digits data without decimal. Can accommodate maximum 11 digit numbers.
float(M,D): Permits real numbers upto M digits, out of which may be D digits after decimal .
Example: a column data type defined as float(6,3) may have 234.684

Date: used to store date in YYYY-MM-DD format.


Data Types in MySQL

Data stored in a database table are of different types, As SQL developers we have chose the suitable data
types for each field while defining a table. SQL offers supports a wide range of data types from which the
developer can choose the most appropriate data types for each column.

char(size): used for fixed length string data.


Example: A column defined as char(10) , it can contain string values of maximum 10 length. SQL allocates 10
bytes of memory irrespective of legth of data to be stored.

varchar(size): used for variable length string data.


Example: If a column defined as varchar(10) , SQL allocates maximum 10 bytes to each value, but bytes
allocated may vary depending on the length of data.
int( ): Used for integer/digits data without decimal. Can accommodate maximum 11 digit numbers.
float(M,D): Permits real numbers upto M digits, out of which may be D digits after decimal .
Example: a column data type defined as float(6,3) may have 234.684

Date: used to store date in YYYY-MM-DD format.


Constraints:

constraints are used to specify rules for the data in a table. Commonly used
constraints are:
Not Null- Ensures that a column cannot have a NULL value
Unique- Ensures that all values in a column are different
Primary Key- A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
Foreign Key- Prevents actions that would destroy links between tables
Check - Ensures that the values in a column satisfies a specific condition
Default- Sets a default value for a column if no value is specified
MySql Commands:

CREATE Database: Used to create a new database.


Syntax: CREATE DATABASE <database name>
e.g. CREATE Database MySchool;

SHOW Databases: Used to list all existing databases.


Syntax: SHOW Databases;

DROP Database: Used to delete an existing database.


Syntax: DROP Database <databasename>
e.g. DROP Database MyStore;

USE Database: Used to select/enter a existing database.


e.g. USE MySchool;
MySql Commands:

Show Tables: After a database has been selected this command can be Used to list all the
tables in the database. e.g. SHOW TABLES;

CREATE Table: Syntax: CREATE TABLE <table name>( column1 datatype, column2 datatype,
column3 datatype, .... columnN datatype,
PRIMARY KEY( one or more columns ) );

E.g. CREATE TABLE cs_students(sid int(3), sname varchar(30), sclass int(2), smark int(3), skill
varchar(30), primary key(sid));

Note: Constraints other then Primary Key can also be specified when required
MySql Commands:

Creating a table with multiple constraints:

CREATE TABLE Employee (Eid int(5) Primary Key,


Ename varchar(30) Not Null,
age int(2),
Dept varchar(20) Default “Manufacturing”,
contactno int(10) unique,
Constraint ChkAge Check(Age>18));
MySql Commands:

DESCRIBE Tables: A DDL command to display the structure of the table.


Syntax: DESCRIBE <table name>;
MySql Commands:

ALTER Tables: ALTER TABLE is a DDL command that can change the structure of the table.
Using ALTER TABLE command we can add, delete or modify the attributes/constraints of a
table.
Adding a column using Alter table:
Syntax: ALTER TABLE <table name> ADD column <Column Name Data type>;

Deleting a column using Alter table:


Syntax: ALTER TABLE <table name> DROP column <Column Name >;
MySql Commands:

Modify a column using Alter table:


Syntax: ALTER TABLE <table name> MODIFY column <Column Name Data type>;
E.g. MODIFY the data type of an existing column

Adding a Primary Key Constraint using Alter table:


Syntax: ALTER TABLE <table name> ADD Primary Key (<column names>);

Drop Primary Key Constraint using Alter table:


Syntax: ALTER TABLE <table name> DROP Primary Key;
MySql Commands:
MySql Commands:

SELECT Command: Used to retrieve and Table: cs_students


display data from the tables.

Syntax: SELECT column1, column2,..


FROM <Table Name>;
E.g. SELECT sid, sname FROM cs_students;
[Here only sid and sname column has been
selected]

To Select all the columns from the table:


SELECT * FROM <Table Name>;
MySql Commands:
Where Clause Examples :

To select ID and Name of the students whose skill is Database:

Using Logical Operators in Where clause:


MySql Commands:
IN Operator: Used To specify multiple possible values for a column
E.g. Select * from cs_student where skill in(“Networking”, ”Database”);

BETWEEN Operator: Used To specify values in a certain range.


E.g. Select * from cs_student where smark BETWEEN 95 AND 100;
MySql Commands:
DISTINCT Clause: Used to retrieve the distinct values in a field.
Syntax: Select * from student where mark is null;

ORDER BY: It is used to sort the data in ascending or descending order. By default ORDER BY sort th
data in ascending order, for descending order we need to use ”DESC”.
MySql Commands:
Handling NULL Values: To handle NULL entries in a field we can use “IS” and “IS NOT”, as NULL
value is a Value which is Unknown so we can not use =, <> operators to select NULL values
MySql Commands:
MySql Commands:
Update Command : UPDATE is a DML command used to change values in the rows of a existing
table. It specifies the rows to be changed using WHERE clause and the new values to be updated
using SET keyword.
Syntax: UPDATE <Table Name> SET column=<new value> WHERE <condition>
E.g. To change the salary to 70000 of the employee having Eid 204.
UPDATE employee SET salary=70000 WHERE Eid=204.
UPDATE employee SET salary=70000,age=32 WHERE Eid=204

To change the Department of an employee


UPDATE employee SET Dept=“Marketing” where Ename=“Kunal”;

Employee Table before Update. Employee Table after Update


Aggregate Functions :
MySql Commands:
MySql Commands: GROUP BY
TABLE: Employee

When used with GROUP BY clause, the aggregate


functions perform calculations on groups of rows (formed by
GROUP BY), and hence returns a single value for each group.

Let us consider the employee table, To find the average salary


of employees of each department, we can use the command.
SELECT dept, avg(salary) from employee GROUP BY dept;

Here the GROUP BY clause creates 3 groups


based on department values and then the
aggregate function avg(salary) is independently
applied to the salary field of each group to
calculate average salary of each group of rows.
MySql Commands: HAVING Clause:
Table: cs_students
HAVING clause is used to apply conditions on
groups in contrast to WHERE clause which is
used to apply conditions on individual rows.
Let us consider the cs_students table, To find the average
marks of the group of students having a particular skill ,
where the skill group must have at least 5 students.

SELECT skill, avg(smark) FROM cs_students GROUP BY skill


HAVING count(*)>=5;
MySql Commands: HAVING Examples

Conditions in having clause may contain a Boolean


expression or aggregate functions can be used to specify the
condition.
We can also use more then one condition in HAVING clause by using
logical operators.
Let us consider the employee table, To find the average
salary of employees of each department, where the average age of all
the employees working in the department is less then 32.
SELECT dept,avg(salary) FROM employee GROUP BY dept HAVING
avg(age)>32
MySql Commands: JOIN
MySql Commands:Cartesian Product (X):
MySql Commands:Cartesian Product (X) Example:
MySql Commands:Equi Join :

To perform Equi/Inner Join on two relations R1 and R2, we have to specify a equality condition using the common
attribute in both the relations R1 and R2. Syntax: SELECT * FROM R1 ,R2 where r1.col1=r2.col1;
MySql Commands: Natural Join :
MySql Commands: Examples on Join
MySql Commands: Examples on Join
MySql Commands: Examples on Join

You might also like