SQL commands For class XII Computer Science _ IP CBSE - Computer Science Tutorial
SQL commands For class XII Computer Science _ IP CBSE - Computer Science Tutorial
Python
0 C Language
Shares
C++
SQL
Sample Papers
10th to 12th
Privacy Policy
1. Create Table
This statement is used to create the structure of a table. We can specify the various fields along wih their data types, size and range of values
which can be stored in those fields. We can also specify the constraints applied on fields.
Syntax:
size specifies the maximum number of digits or characters which can be stored in a particular field. It is optional in case of number data type.
name varchar 20
dateofbirth date
Class varchar 20
Create database Class 12 Computer Science 2022 | Create table Class 12 Computer Science 2022 hindi
0
Shares
2. Alter Table
This statement is used to modify the structure of a table. We can add or modify fields of an existing table.
We can also specify the data type, size, constraints and domain of fields which are to be added or modified in an existing table.
We can increase the size of any field at any time but we should be careful while decreasing size of any field as it may result in data loss.
Syntax 1:
0
Example 1:
Shares
Above command will add a new field address to table named student.
Example 2:
Above command will add a unique constraint on address field of student table.
Syntax 2:
Example
Above command will increase the width of field named “name” to 25.
Syntax 3:
Example:
SQL DDL Statements | SQL DDL statements Class 12 Computer Science 2022
0
Shares
3. Drop Table
This statement is used to delete the structure of a table as well as data stored in a particular table.
Syntax:
Example
1. Insert
This command used to insert a new record in an existing table. We can insert a complete record or we can enter record values in specific fields
also.
Syntax:
Insert into table_name(field1,field2,……….)
values (datavalue1,datavalue2, ………..);
0
Here table_name is the name of an existing table
Shares
field1,field2… represent the names of various fields of table
datavalue1,datavalue2, … represent the values which are to be inserted
into field1,field2,…….
Example 1:
Above statement will insert a complete record in table named student. 101 will store in roll field, ‘amit’ in name field, ‘2000-01-05’ in dateofbirth
field and 5000.50 in fees field.
Example 2:
Above will only insert values in two fields named roll and name. 102 will be stored in roll field, ‘sumit’ in name field, dateofbirth and fees fields
will get null.
2. update
This command is used to modify an exiting record . We can modify more than one field at a time and we can also modify those records which
satisfy a particular condition.
Syntax: –
Update <table_name>
set column_name1= expression1 , column_name2= expression2 , ……..
where <condition >;
Example 1:
Above statement ill make all the values in roll field to be 106.
Example 2:
Update student set fees=fees+100;
Above statement will increase the roll by 10 in those records where fees>4500.
3. Delete
This command is used to delete all the records or an exiting record from a database table. Structure of table will not be deleted.
Syntax:
Example 1
Above command will delete all the records from table named student.
Example 2
Above command will delete those records from table named student which is having fees more than 4000.
DML statements of SQL Class 12 Computer Science 2022
0
Shares
4. Select
This command is used to view all the records or specific records from a database table depending on a particular condition.
Syntax:
Here table_name is the name of an existing table * represents that all the records from the table should be shown on the screen. column1,column2
represent the names of fields whose contents are to be shown. condition is any relational or logical expression which is used to specify the
condition as per which records should be deleted.
Example 1
Above command will show all the records from table named student.
Example 2
0
Shares
Above command will show contents of fields named roll and name for those records from table named student which have fees more than 4000.
Example 3
Above command will show all those records from table named student which have fees more than 4000.
1. DISTINCT Clause
This clause is used with select command to view Unique values in a particular field.
Example
This command will show different values in the class field of student table avoiding duplicate values.
2. IN Clause
This clause is used to specify a range of values. This clause is used with select, delete , update statements.
Example
This command is used to view those records where class field of student table contains either ‘xi’ or ‘xii’
3. BETWEEN Clause
The BETWEEN clause allows to check if an expression is within a range of two values. The values can be text, date, or numbers. The BETWEEN
clause used in a condition will return the records where value of expression is within the range of value1 and value2.
Example:
OR
In the above examples, those records from table student will be shown where value of fees is between 1000 and 2000. Both values 1000 and 2000
are included in the output.
4. LIKE Clause
The LIKE clause is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards used with LIKE clause:
Examples
WHERE Name LIKE ‘A%’ ==> Finds values that start with “A”
WHERE Name LIKE ‘%s’ ==> Finds values that end with “s”
WHERE Name LIKE ‘%or%’ ==> Finds values that have “or” in any position
WHERE Name LIKE ‘_a%’ ==> Finds values that have “a” in the second position
WHERE Name LIKE ‘a_%’ ==> Finds values that start with “a” and are at least 2 characters in length
WHERE Name LIKE ‘a__%’ ==> Finds values that start with “a” and are at least 3 characters in length
WHERE Name LIKE ‘a%s’ ==> Finds any values that start with “a” and ends with “s”
This command will show all those records from table named student where name field contains values starting with A for e.g. Arun, Aman, Anu ,
Amandeep etc.
This command will show all those records from table student where name field contains values containing RU in it for e.g. ARUN, VARUN,
KARUNA.
This command will show all those records from table student where name field contains values starting with A and having four characters for e.g.
Arun, Aman etc.
This command will show all those records from table student where name field contains values starting from A and ending with N and four
characters in it for e.g. ARUN, AMAN etc.
5. IS NULL Clause
NULL means empty. It is neither Zero nor blank but it means “not defined”. This clause is used with select command to view those records which
contain null values in a particular field i.e Field doesn’t contain any value in various records.
Example
0
Shares
i.. select * from student where fees is null;
This command will show all the records from student table where fees field contains null value.
This command will show all the records from student table where fees field does not contain null value.
6. ORDER BY Clause
This clause is basically used to sort the records as per a particular field. It is used with select command to view records alphabetically or
numerically in ascending order or descending order with respect to a particular field. We can user word “asc” to specify that records should be in
ascending order. It is the default order when when we user order by clause. We use “desc” with order by clause to show the records in descending
order of a particular field.
Example
OR
This command will show all the records from student table in ascending order of field “name”.
This command will show all the records from student table in descending order of field “name”.
7. GROUP BY Clause
This clause can be used with select statement. This clause is used to group records based on distinct values that exist for specified columns i.e. it
creates a data set containing several sets of records grouped together based on a condition. We can specify the condition for which records should
be grouped by using “having” clause.
Example:
This command will show the classes along with sum of fees corresponding to various classes in the table student.
ii. Select class, sum(fees) from student group by job having count(class)>3;
This command will show the classes along with sum of fees corresponding to various classes where number of records corresponding to a
0
particular class are more than 3.
Shares
SQL Select statement class 12 Computer Science 2022 | Select statement of SQL class 12
SQL Functions
i. SUM
This function is used to find the sum of values stored in a numeric field of a table.
Syntax:
SUM(Field_Name)
Field_Name is the name of any numeric field on which we want to apply sum Function.
Example:
select sum(fees) from student;
This command will compute the sum of values stored in fees field of student table.
0
Shares
ii AVG:
This function is used to find the average of values stored in a numeric field of a table.
Syntax:
AVG(Field_Name)
Field_Name is the name of any numeric field on which we want to apply avg Function.
Example:
This command will compute the average of values stored in fees field of student table.
iii. MIN
This function is used to find the minimum value among the values stored in a numeric field of a table.
Syntax:
MIN(Field_Name)
Field_Name is the name of any numeric field on which we want to apply MIN Function.
Example:
iv. MAX
This function is used to find the maximum value among the values stored in a numeric field of a table.
Syntax:
MAX(Field_Name)
Field_Name is the name of any numeric field on which we want to apply MIN Function.
Example:
0
Shares
This command will find maximum fees from student table.
v. COUNT
This function is used to count the number of records in a particular field or in complete table of Oracle.
Syntax
count(Expression/*)
Expression may be the name of any field of table or any expression based on a field of a table. * is used when we want to know the number of
records in a table.
Example
This command will show the number of non null values stored in fees field of student table.
This command will show the total number of records stored in student table.
Aggregate functions of SQL | Group by clause of SQL | aggregate functions of SQL class 12 hindi
0
Shares
SQL Joins
It is the way to combine records of more than one table in which there must exist one common field on which we create a relation between two
I. EQUI JOIN
This type of join is used to combine records from tables where the common field of both tables have same value. Equi join is a special type of join
in which we use only an equality (=) operator to select only those records having same value in common field.
Example:
Table: Foods
+---------+--------------+-----------+
| ITEM_ID | ITEM_NAME | COMPANY_ID|
+---------+--------------+-----------+
| 1 | Chex Mix | 16 |
| 6 | Cheez-It | 15 |
| 2 | BN Biscuit | 15 |
| 4 | Pot Rice | 15 |
+---------+--------------+-----------+
Table: Company
+------------+---------------+
| COMPANY_ID | COMPANY_NAME |
+------------+---------------+
| 15 | Jack Hill |
| 16 | Akas Foods |
| 17 | Foodies. |
| 19 | sip-n-Bite. |
+------------+---------------+
0
Shares
SELECT FOODS.ITEM_ID,FOOD.ITEM_NAME,
COMPANY.COMPANY_ID,COMPANY.COMPANY_NAME
FROM FOODS,COMPANY
WHERE FOODS.COMPANY_ID=COMPANY.COMPANY_ID;
OUTPUT
+---------+--------------+-----------+--------------+
| ITEM_ID | ITEM_NAME | COMPANY_ID| COMPANY_NAME |
+---------+--------------+-----------+--------------+
| 1 | Chex Mix | 16 | Akas Foods |
| 6 | Cheez-It | 15 | Jack Hill |
| 2 | BN Biscuit | 15 | Jack Hill |
| 4 | Pot Rice | 15 | Jack Hill |
+---------+--------------+-----------+--------------+
Output will show only those records from both the tables where company_id is same.
A natural join is a type of equi join which occurs implicitly by comparing all the same names columns in both tables. The join result has only one
The tables to be joined must have one or more columns having same name.
The columns must be the same data type.
Syntax:
SELECT *
FROM table1
NATURAL JOIN table2;
Example:
Table: Foods
+---------+--------------+-----------+
| ITEM_ID | ITEM_NAME | COMPANY_ID|
+---------+--------------+-----------+
| 1 | Chex Mix | 16 |
| 6 | Cheez-It | 15 |
| 2 | BN Biscuit | 15 |
| 4 | Pot Rice | 15 |
+---------+--------------+-----------+
0
Shares
Table: Company
+------------+---------------+
| COMPANY_ID | COMPANY_NAME |
+------------+---------------+
| 15 | Jack Hill |
| 16 | Akas Foods |
| 17 | Foodies. |
| 19 | sip-n-Bite. |
+------------+---------------+
SELECT *
FROM foods NATURAL JOIN company;
OUTPUT
+---------+--------------+-----------+--------------+
| ITEM_ID | ITEM_NAME | COMPANY_ID| COMPANY_NAME |
+---------+--------------+-----------+--------------+
| 1 | Chex Mix | 16 | Akas Foods |
| 6 | Cheez-It | 15 | Jack Hill |
| 2 | BN Biscuit | 15 | Jack Hill |
| 4 | Pot Rice | 15 | Jack Hill |
+---------+--------------+-----------+--------------+
Output will show only those records from both the tables where company_id is same.
Natural Join in SQL class 12 computer science | Equi Join in SQL class 12 Computer Science hindi
0
Shares
Quizzes
0
Shares
Lesson tags: ALTER TABLE, alter table command of sql, clause used with select command of sql, CREATE TABLE, create table command of sql, ddl commands of
sql, delete command of sql, dml commands of sql, DROP TABLE, drop table command of sql, group functions of sql, insert command of sql, select command of
sql, SQL commands, update command of sql
0
Shares