0% found this document useful (0 votes)
4 views12 pages

PRDBMS All Practice Code

The document contains SQL commands for creating and manipulating tables related to fruits, students, suppliers, and products. It includes operations such as inserting, updating, deleting records, and various SQL functions for data retrieval and formatting. Additionally, it demonstrates the use of aggregate functions and table alterations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

PRDBMS All Practice Code

The document contains SQL commands for creating and manipulating tables related to fruits, students, suppliers, and products. It includes operations such as inserting, updating, deleting records, and various SQL functions for data retrieval and formatting. Additionally, it demonstrates the use of aggregate functions and table alterations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

INPUT:~

create table fruits(id int(3), name varchar(20), price decimal(5,2), season


set('Winter','Summer','Monsoon'));

describe fruits;

insert into fruits values(1,"Mango",60.15,"Summer"),(2,"Guava",80.72,"Winter"),


(3,"Pomogranate",100.45,"Monsoon");

select * from fruits;

create table students(RollNo varchar(10), name varchar(20), gender enum('male','female'), hobby


set('dancing','singing','drawing','music'));

describe students;

insert into students values(18,"Anushka","female","music"),(37,"Krisha","female","drawing"),


(12,"Aryan","male","singing");

select * from students;

select name,hobby from students where RollNo=37;

update students set hobby = "dancing" where RollNo = "18";

select * from students;

delete from fruits where season="Winter";

select * from fruits;


select RollNo,name from students where name like"A_%";

select ascii('7');

select bin('13');

select bit_length('keval');

select char_length('dhorajiya');

select concat('sm','i','t');

select concat_ws('*','smit','gadhiya');

select conv('d',16,2);

select find_in_set('a','p,r,a,c,h,i');

select format(12843.98765,3);

select hex('77');

select insert('keval',2,4,'dhorajiya');

select instr('dhorajiya','ji');

select lower('KEVAL');

select left('dhorajiya',5);

select right('dhorajiya',4);

select length('gadhiya');

select locate('ya','dhorajiya');

select lpad('ke',4,'val');

select rpad('ke',4,'val');

select ltrim(' smit');

select rtrim('smit ');


select oct(13);

select repeat('smit',3);

select replace('smit','s','fs');

select reverse('gadhiya');

select strcmp('keval','KEVAL');

select substring('typewriter',5);

select substring('typewriter' from 6);

select substring('typewriter',3,4);

select * from students where name in('Aryan','Krisha');

select * from students where name not in('Aryan','Krisha');

CREATE TABLE suppliers (

Supplier_ID INT(5) PRIMARY KEY,

Name VARCHAR(50),

Contact DECIMAL(10)

);

describe suppliers;

CREATE TABLE Sproduct (

Product_ID INT(10),

Code VARCHAR(50),

Name VARCHAR(50),

Quantity INT(10),
Supplier_ID INT(10),

Price DECIMAL(7,2),

FOREIGN KEY (Supplier_ID) REFERENCES suppliers(Supplier_ID)

);

describe Sproduct;

INSERT INTO suppliers VALUES (1, 'Supplier A', '75321489'),

(2, 'Supplier B', '98765210'),

(3, 'Supplier C', '45689120'),

(4, 'Supplier D', '78912560'),

(5, 'Supplier E', '31654970');

INSERT INTO Sproduct VALUES (1, 'SP001', 'Product A', 20, 1, 1000.00),

(2, 'SP002', 'Product B', 15, 2, 1500.00),

(3, 'SP003', 'Product C', 25, 3, 2000.00),

(4, 'SP004', 'Product D', 10, 4, 2500.00),

(5, 'SP005', 'Product E', 5, 5, 3000.00);

select * from suppliers;

select * from Sproduct;

select* from Sproduct where(Quantity between 10 and 20);

CREATE TABLE random_table (

ID INT(5) PRIMARY KEY AUTO_INCREMENT,

Random_Name VARCHAR(50),
Random_Value INT(5));

INSERT INTO random_table VALUES(5,'Alpha', 100);

INSERT INTO random_table (Random_Name, Random_Value) VALUES('Beta', 200);

INSERT INTO random_table (Random_Name, Random_Value) VALUES('Gamma', 300);

INSERT INTO random_table (Random_Name, Random_Value) VALUES('Delta', 400);

INSERT INTO random_table (Random_Name, Random_Value) VALUES('Epsilon', 500);

select * from random_table;

select * from random_table limit 2,3;

select Random_Name as Name from random_table;

select max(price) as maximum from Sproduct;

select max(price),min(price),avg(price),count(price),sum(price) from Sproduct;

select * from Sproduct order by Quantity asc;

select * from Sproduct order by Quantity desc;

select count(*) from fruits season;

select count(*) as Count,gender from students group by gender;

select distinct gender from students;

alter table random_table drop column Random_Value;


select * from random_table;

alter table random_table modify column ID int(7);

describe random_table;

alter table random_table add column mfg_year year;

select * from random_table;

rename table random_table to random;

show tables;

OUTPUT:~
+--------+----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------------------------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| price | decimal(5,2) | YES | | NULL | |
| season | set('Winter','Summer','Monsoon') | YES | | NULL | |
+--------+----------------------------------+------+-----+---------+-------+
+------+-------------+--------+---------+
| id | name | price | season |
+------+-------------+--------+---------+
| 1 | Mango | 60.15 | Summer |
| 2 | Guava | 80.72 | Winter |
| 3 | Pomogranate | 100.45 | Monsoon |
+------+-------------+--------+---------+
+--------+--------------------------------------------+------+-----+---------
+-------+
| Field | Type | Null | Key | Default |
Extra |
+--------+--------------------------------------------+------+-----+---------
+-------+
| RollNo | varchar(10) | YES | | NULL |
|
| name | varchar(20) | YES | | NULL |
|
| gender | enum('male','female') | YES | | NULL |
|
| hobby | set('dancing','singing','drawing','music') | YES | | NULL |
|
+--------+--------------------------------------------+------+-----+---------
+-------+
+--------+---------+--------+---------+
| RollNo | name | gender | hobby |
+--------+---------+--------+---------+
| 18 | Anushka | female | music |
| 37 | Krisha | female | drawing |
| 12 | Aryan | male | singing |
+--------+---------+--------+---------+
+--------+---------+
| name | hobby |
+--------+---------+
| Krisha | drawing |
+--------+---------+
+--------+---------+--------+---------+
| RollNo | name | gender | hobby |
+--------+---------+--------+---------+
| 18 | Anushka | female | dancing |
| 37 | Krisha | female | drawing |
| 12 | Aryan | male | singing |
+--------+---------+--------+---------+
+------+-------------+--------+---------+
| id | name | price | season |
+------+-------------+--------+---------+
| 1 | Mango | 60.15 | Summer |
| 3 | Pomogranate | 100.45 | Monsoon |
+------+-------------+--------+---------+
+--------+---------+
| RollNo | name |
+--------+---------+
| 18 | Anushka |
| 12 | Aryan |
+--------+---------+
+------------+
| ascii('7') |
+------------+
| 55 |
+------------+
+-----------+
| bin('13') |
+-----------+
| 1101 |
+-----------+
+---------------------+
| bit_length('keval') |
+---------------------+
| 40 |
+---------------------+
+--------------------------+
| char_length('dhorajiya') |
+--------------------------+
| 9 |
+--------------------------+
+----------------------+
| concat('sm','i','t') |
+----------------------+
| smit |
+----------------------+
+---------------------------------+
| concat_ws('*','smit','gadhiya') |
+---------------------------------+
| smit*gadhiya |
+---------------------------------+
+----------------+
| conv('d',16,2) |
+----------------+
| 1101 |
+----------------+
+--------------------------------+
| find_in_set('a','p,r,a,c,h,i') |
+--------------------------------+
| 3 |
+--------------------------------+
+-----------------------+
| format(12843.98765,3) |
+-----------------------+
| 12,843.988 |
+-----------------------+
+-----------+
| hex('77') |
+-----------+
| 3737 |
+-----------+
+---------------------------------+
| insert('keval',2,4,'dhorajiya') |
+---------------------------------+
| kdhorajiya |
+---------------------------------+
+-------------------------+
| instr('dhorajiya','ji') |
+-------------------------+
| 6 |
+-------------------------+
+----------------+
| lower('KEVAL') |
+----------------+
| keval |
+----------------+
+---------------------+
| left('dhorajiya',5) |
+---------------------+
| dhora |
+---------------------+
+----------------------+
| right('dhorajiya',4) |
+----------------------+
| jiya |
+----------------------+
+-------------------+
| length('gadhiya') |
+-------------------+
| 7 |
+-------------------+
+--------------------------+
| locate('ya','dhorajiya') |
+--------------------------+
| 8 |
+--------------------------+
+--------------------+
| lpad('ke',4,'val') |
+--------------------+
| vake |
+--------------------+
+--------------------+
| rpad('ke',4,'val') |
+--------------------+
| keva |
+--------------------+
+-----------------+
| ltrim(' smit') |
+-----------------+
| smit |
+-----------------+
+------------------+
| rtrim('smit ') |
+------------------+
| smit |
+------------------+
+---------+
| oct(13) |
+---------+
| 15 |
+---------+
+------------------+
| repeat('smit',3) |
+------------------+
| smitsmitsmit |
+------------------+
+--------------------------+
| replace('smit','s','fs') |
+--------------------------+
| fsmit |
+--------------------------+
+--------------------+
| reverse('gadhiya') |
+--------------------+
| ayihdag |
+--------------------+
+-------------------------+
| strcmp('keval','KEVAL') |
+-------------------------+
| 0 |
+-------------------------+
+---------------------------+
| substring('typewriter',5) |
+---------------------------+
| writer |
+---------------------------+
+--------------------------------+
| substring('typewriter' from 6) |
+--------------------------------+
| riter |
+--------------------------------+
+-----------------------------+
| substring('typewriter',3,4) |
+-----------------------------+
| pewr |
+-----------------------------+
+--------+--------+--------+---------+
| RollNo | name | gender | hobby |
+--------+--------+--------+---------+
| 37 | Krisha | female | drawing |
| 12 | Aryan | male | singing |
+--------+--------+--------+---------+
+--------+---------+--------+---------+
| RollNo | name | gender | hobby |
+--------+---------+--------+---------+
| 18 | Anushka | female | dancing |
+--------+---------+--------+---------+
+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| Supplier_ID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Contact | decimal(10,0) | YES | | NULL | |
+-------------+---------------+------+-----+---------+-------+
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| Product_ID | int | YES | | NULL | |
| Code | varchar(50) | YES | | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Quantity | int | YES | | NULL | |
| Supplier_ID | int | YES | MUL | NULL | |
| Price | decimal(7,2) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
+-------------+------------+----------+
| Supplier_ID | Name | Contact |
+-------------+------------+----------+
| 1 | Supplier A | 75321489 |
| 2 | Supplier B | 98765210 |
| 3 | Supplier C | 45689120 |
| 4 | Supplier D | 78912560 |
| 5 | Supplier E | 31654970 |
+-------------+------------+----------+
+------------+-------+-----------+----------+-------------+---------+
| Product_ID | Code | Name | Quantity | Supplier_ID | Price |
+------------+-------+-----------+----------+-------------+---------+
| 1 | SP001 | Product A | 20 | 1 | 1000.00 |
| 2 | SP002 | Product B | 15 | 2 | 1500.00 |
| 3 | SP003 | Product C | 25 | 3 | 2000.00 |
| 4 | SP004 | Product D | 10 | 4 | 2500.00 |
| 5 | SP005 | Product E | 5 | 5 | 3000.00 |
+------------+-------+-----------+----------+-------------+---------+
+------------+-------+-----------+----------+-------------+---------+
| Product_ID | Code | Name | Quantity | Supplier_ID | Price |
+------------+-------+-----------+----------+-------------+---------+
| 1 | SP001 | Product A | 20 | 1 | 1000.00 |
| 2 | SP002 | Product B | 15 | 2 | 1500.00 |
| 4 | SP004 | Product D | 10 | 4 | 2500.00 |
+------------+-------+-----------+----------+-------------+---------+
+----+-------------+--------------+
| ID | Random_Name | Random_Value |
+----+-------------+--------------+
| 5 | Alpha | 100 |
| 6 | Beta | 200 |
| 7 | Gamma | 300 |
| 8 | Delta | 400 |
| 9 | Epsilon | 500 |
+----+-------------+--------------+
+----+-------------+--------------+
| ID | Random_Name | Random_Value |
+----+-------------+--------------+
| 7 | Gamma | 300 |
| 8 | Delta | 400 |
| 9 | Epsilon | 500 |
+----+-------------+--------------+
+---------+
| Name |
+---------+
| Alpha |
| Beta |
| Gamma |
| Delta |
| Epsilon |
+---------+
+---------+
| maximum |
+---------+
| 3000.00 |
+---------+
+------------+------------+-------------+--------------+------------+
| max(price) | min(price) | avg(price) | count(price) | sum(price) |
+------------+------------+-------------+--------------+------------+
| 3000.00 | 1000.00 | 2000.000000 | 5 | 10000.00 |
+------------+------------+-------------+--------------+------------+
+------------+-------+-----------+----------+-------------+---------+
| Product_ID | Code | Name | Quantity | Supplier_ID | Price |
+------------+-------+-----------+----------+-------------+---------+
| 5 | SP005 | Product E | 5 | 5 | 3000.00 |
| 4 | SP004 | Product D | 10 | 4 | 2500.00 |
| 2 | SP002 | Product B | 15 | 2 | 1500.00 |
| 1 | SP001 | Product A | 20 | 1 | 1000.00 |
| 3 | SP003 | Product C | 25 | 3 | 2000.00 |
+------------+-------+-----------+----------+-------------+---------+
+------------+-------+-----------+----------+-------------+---------+
| Product_ID | Code | Name | Quantity | Supplier_ID | Price |
+------------+-------+-----------+----------+-------------+---------+
| 3 | SP003 | Product C | 25 | 3 | 2000.00 |
| 1 | SP001 | Product A | 20 | 1 | 1000.00 |
| 2 | SP002 | Product B | 15 | 2 | 1500.00 |
| 4 | SP004 | Product D | 10 | 4 | 2500.00 |
| 5 | SP005 | Product E | 5 | 5 | 3000.00 |
+------------+-------+-----------+----------+-------------+---------+
+----------+
| count(*) |
+----------+
| 2 |
+----------+
+-------+--------+
| Count | gender |
+-------+--------+
| 2 | female |
| 1 | male |
+-------+--------+
+--------+
| gender |
+--------+
| female |
| male |
+--------+
+----+-------------+
| ID | Random_Name |
+----+-------------+
| 5 | Alpha |
| 6 | Beta |
| 7 | Gamma |
| 8 | Delta |
| 9 | Epsilon |
+----+-------------+
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| ID | int | NO | PRI | NULL | |
| Random_Name | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
+----+-------------+----------+
| ID | Random_Name | mfg_year |
+----+-------------+----------+
| 5 | Alpha | NULL |
| 6 | Beta | NULL |
| 7 | Gamma | NULL |
| 8 | Delta | NULL |
| 9 | Epsilon | NULL |
+----+-------------+----------+
+----------------------------------+
| Tables_in_sandbox_db |
+----------------------------------+
| fruits |
| random |
| sproduct |
| students |
| suppliers |
+----------------------------------+

You might also like