0% found this document useful (0 votes)
2 views17 pages

Lab Ex 1

The document provides a comprehensive guide on using SQLite, including commands for creating and managing databases, tables, and transactions. It includes examples of SQL commands for inserting data, checking types, and performing operations like rollback and commit. Additionally, it covers the creation of various tables and records, as well as using SQL clauses such as DISTINCT and WHERE for filtering data.

Uploaded by

rathodbhaumik3
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)
2 views17 pages

Lab Ex 1

The document provides a comprehensive guide on using SQLite, including commands for creating and managing databases, tables, and transactions. It includes examples of SQL commands for inserting data, checking types, and performing operations like rollback and commit. Additionally, it covers the creation of various tables and records, as well as using SQL clauses such as DISTINCT and WHERE for filtering data.

Uploaded by

rathodbhaumik3
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/ 17

Lab Exercise 1

1) To work in sqlite3
Open command prompt
Type sqlite3 and press enter
2) To open & create database
Type sqlite3 databasename.db
sqlite3 student.db
3) To list name of all created databases
Type .databases
4) To list name of all created tables
Type .tables
5) To check Dynamic type / Manifest Type

To create table
CREATE TABLE a(i INTEGER, n numeric, t text, b blob);

To insert data
sqlite> INSERT INTO a values(9.581,9.581,9.581,9.581);
sqlite> INSERT INTO a values('9.581','9.581','9.581','9.581');
sqlite> INSERT INTO a values(9581,9581,9581,9581);

To check dynamic type


SELECT ROWID,typeof(i),typeof(n),typeof(t),typeof(b) FROM a;

To display records
sqlite> select * from a;

9.581|9.581|9.581|9.581
9.581|9.581|9.581|9.581
9581|9581|9581|9581

Example 2:
sqlite> create table s(rno primary key,nm,marks);
sqlite> insert into s values(1,'Het',90),(2,'Viral',85),(3,'Devsay',92);
sqlite> select * from s;

Dr. Jyoti Rana


Naran Lala College, Navsari Page 1
1|Het|90
2|Viral|85
3|Devsay|92
6) Create database named prac1 in sqlite3:
.open prac1.db
7) To check Transactions
.open emp.db
.databases
create table emp(eno,enm,city);
insert into emp values(1,'Gaurav','Navsari'), (2,'Vishva','Surat'),
(3,'Darshit','Vapi'), (4,'Dev','Valsad'), (5,'Dhruv','Surat');

.mode box
select * from emp;

Example of rollback
sqlite> begin;
sqlite> insert into emp values(6,'Dhruvi','Vapi');
sqlite> select * from emp;
┌─────┬─────────┬─────────┐
│ eno │ enm │ city │
├─────┼─────────┼─────────┤
│ 1 │ Gaurav │ Navsari │
│ 2 │ Vishva │ Surat │
│ 3 │ Darshit │ Vapi │
│ 4 │ Dev │ Valsad │
│ 5 │ Dhruv │ Surat │
│ 6 │ Dhruvi │ Vapi │
└─────┴─────────┴─────────┘
sqlite> rollback;
sqlite> select * from emp;
┌─────┬─────────┬─────────┐
│ eno │ enm │ city │
Dr. Jyoti Rana
Naran Lala College, Navsari Page 2
├─────┼─────────┼─────────┤
│ 1 │ Gaurav │ Navsari │
│ 2 │ Vishva │ Surat │
│ 3 │ Darshit │ Vapi │
│ 4 │ Dev │ Valsad │
│ 5 │ Dhruv │ Surat │
└─────┴─────────┴─────────┘

Example of commit
sqlite> begin;
sqlite> insert into emp values(6,'Dhruvi','Vapi');
sqlite> insert into emp values(7,'Diya','Valsad');
sqlite> commit;
sqlite> select * from emp;
┌─────┬─────────┬─────────┐
│ eno │ enm │ city │
├─────┼─────────┼─────────┤
│ 1 │ Gaurav │ Navsari │
│ 2 │ Vishva │ Surat │
│ 3 │ Darshit │ Vapi │
│ 4 │ Dev │ Valsad │
│ 5 │ Dhruv │ Surat │
│ 6 │ Dhruvi │ Vapi │
│ 7 │ Diya │ Valsad │

8) Create following Tables inside db1 database as well as insert necessary


records into it:
1. Dept_master
Dept_id Integer (Primary key)
Dept_name Text

Depttbl.sql
Dr. Jyoti Rana
Naran Lala College, Navsari Page 3
CREATE TABLE dept_master(dept_id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_name TEXT);
To execute the query

.read depttbl.sql
.mode box
Select * from dept_master;

Deptins.sql
Insert following records into it:

Dept_id Dept_name
1 Admin
2 Sales
3 Quality Control
4 Marketing

insert into dept_master values(1,'Admin'),(2,'Sales'),(3,'Quality


Control'),(4,'Marketing');

2. Emp_master (Transaction Commands)

Emp_id Integer (Primary key)


First_name Text
Last_name Text
Salary Numeric
Joining_date date
Dept_id Integer (References
toDept_id of
Dept_master)

emptbl.sql

create table emp_master(emp_id integer primary key autoincrement,


first_name text,

Dr. Jyoti Rana


Naran Lala College, Navsari Page 4
last_name text,
salary numeric,
jdate date,
dept_id integer references dept_master(dept_id));

empins.sql
Insert following records into it:
Emp_id First_name Last_name Salary Joining_date Dept_id
1 Dev Rana 50000 1-1-2010 1
2 Krishna Patel 30000 5-4-2021 1
3 Jhanvi Rana 70000 3-10-2006 1
4 Aditi Patel 65000 9-11-1979 2
5 Aryan Patel 55000 11-12-2005 2
6 Ahana Rastogi 44000 5-1-2003 3
7 Niti Gandhi 80000 18-8-2004 3
8 Dev Shah 75000 26-6-2009 3
9 Krishna Shah 60000 1-5-2010 2
10 Swayam Gandhi 20000 16-5-2010 1

INSERT INTO emp_master


values (1,'Dev','Rana', 50000,'1-1-2010',1),
(2,'Krishna','Patel', 30000,'5-4-2021',1),
(3,'Jhanvi', 'Rana',70000,'3-10-2006',1),
(4, 'Aditi','Patel', 65000,'9-11-1979',2),
(5, 'Aryan ','Patel',55000,'11-12-2005',2),
(6, 'Ahana', 'Rastogi', 44000,'5-1-2003',3),
(7, 'Niti','Gandhi', 80000,'18-8-2004',3),
(8,'Dev','Shah', 75000,'26-6-2009',3),
(9,'Krishna','Shah', 60000,'1-5-2010',2),
(10,'Swayam','Gandhi', 20000, '16-5-2010',1);

3. Student (For Filtering Clause)


Roll no Integer

Dr. Jyoti Rana


Naran Lala College, Navsari Page 5
Name Text
Surname Text
City Text
Marks (out of 100) Float

Create table student(rollno integer primary key,


Name text not null,
Surname text,
City text,
Marks float);
Roll NO Name Surname City Marks
1 Aditya Patel Navsari 90
2 Akash Gandhi Surat 80
3 Jiya Rana Valsad 85
4 Aditi Patel Surat 70
5 Devanshi Rana Vapi 82
6 Ahana Rastogi Mumbai 95
7 Aditya Shah Surat
8 Mehul Patel Navsari

insert into student values(1,'Aditya','Patel','Navsari',90),


(2,'Akash','Gandhi','Surat',80),
(3,'Jiya','Rana','Valsad',85),
(4,'Aditi','Patel','Surat',70),
(5,'Devanshi','Rana','Vapi',82),
(6,'Ahana','Rastogi','Mumbai',95),
(7,'Aditya','Shah','Surat',null),
(8, 'Mehul ', 'Patel', 'Navsari',null);
4. Vegetable
Name Text
Color Text

Create table vegetable(name text, color text);


Records
Dr. Jyoti Rana
Naran Lala College, Navsari Page 6
Name Color
Peas Green
Carrot Orange
Cucumber Green
Tomato Red
Capsicum Green

insert into vegetable


values('Peas','Green'),('Carrot','Orange'),('Cucumber','Green'),
('Tomato','Red'),('Capcicum','Green');

5. Cat
Type Text
Color Text

Create table cat(type text, color text);

Records
Type Color
Indoor white
Outdoor black

insert into cat values('Indoor','white'),('Outdoor','black');


6. Dog
Type Text
Color Text
Create table dog(type text, color text);

Records
Type Color
Haunting Black
Guard brown
insert into dog values('Haunting','Black'),('Guard','brown');

Dr. Jyoti Rana


Naran Lala College, Navsari Page 7
(For Triggers)
7. Product
Pid Integer
Pname Text
Amount Real
Quantity Integer
Create table product(pid integer,pname text,amount real,quantity integer);
Records
Pid Pname Amount Quantity
101 Mouse 500 5
102 Keyboard 2000 10
103 Bluetooth 5000 50
104 Speakers 8000 30
Insert into product
values(101,’Mouse’,500,5),(102,’Keyboard’,2000,10),(103,’Bluetooth’,5000,
50),(104,’Speakers’,8000,30);
8. Product_log (only create table, Don’t insert records into this table)
Pid Integer
Operation Text
Old_amount Real
New_amount Real
Old_pname Text
New_pname Text
Create table product_log(pid integer,operation text,old_amount
real,new_amount real,old_pname text,new_pname text);
9. Publisher
Pub_id Integer
Pname Text

Records
Pub_id Pname
1 Tata Mc Graw hill
2 Edward

Dr. Jyoti Rana


Naran Lala College, Navsari Page 8
3 Popular
4 Herber Schildt

10.Book
Book_id Integer
Name Text
Price Real
Pub_id Integer

Records
Book_id Name Price Pub_id
1 Java 2000 1
Programming
2 Python Complete 3000 2
Reference
3 Sqlite Python 5000 1
4 Android 2500 1
Fundamentals

Queries

SQLite DISTINCT clause

It is used with SELECT statement to eliminate all the duplicate


records and fetching only unique records. It is used when you have
multiple duplicate records in the table.
Syntax:
1. SELECT DISTINCT column1, column2,. ....... columnN
2. FROM table_name
3. WHERE [condition]

Example:

Select distinct name from student;

Aditya
Akash

Dr. Jyoti Rana


Naran Lala College, Navsari Page 9
Jiya
Aditi
Devanshi
Ahana

SQLite where clause

It is generally used with SELECT, UPDATE and DELETE statement to


specify a condition while you fetch the data from one table or multiple
tables.
If the condition is satisfied or true, it returns specific value from
the table. We can use WHERE clause to filter the records and
fetching only necessary records.
WHERE clause is also used to filter the records and fetch only
specific data.
Syntax:
1. SELECT column1, column2, columnN
2. FROM table_name
3. WHERE [condition]

Example:

Select * from student;


┌────────┬──────────┬─────────┬─────────┬───────┐
│ rollno │ Name │ Age │ City │ Marks │
├────────┼──────────┼─────────┼─────────┼───────┤
│1 │ Aditya │ Patel │ Navsari │ 90.0 │
│2 │ Akash │ Gandhi │ Surat │ 80.0 │
│3 │ Jiya │ Rana │ Valsad │ 85.0 │
│4 │ Aditi │ Patel │ Surat │ 70.0 │
│5 │ Devanshi │ Rana │ Vapi │ 82.0 │
│6 │ Ahana │ Rastogi │ Mumbai │ 95.0 │
│7 │ Aditya │ Shah │ Surat │ 93.0 │
└────────┴──────────┴─────────┴─────────┴───────┘

To check more than one conditions

SELECT * FROM STUDENT WHERE Name='Aditya' and City='Navsari';

┌────────┬────────┬───────┬─────────┬───────┐
│ rollno │ Name │ Age │ City │ Marks │

Dr. Jyoti Rana


Naran Lala College, Navsari Page 10
├────────┼────────┼───────┼─────────┼───────┤
│1 │ Aditya │ Patel │ Navsari │ 90.0 │
└────────┴────────┴───────┴─────────┴───────┘

SQLite Like clause


It is pattern matching operator that is used to match specific
patterns within the columns. It is mainly used to search the
values/patterns in specified columns using wildcards.

Wildcard characters are characters that can be used for searching,


filtering and retrieving data that follows same structure.

% - used to match 0 or more number of occurrences in the columns


_ - used to match one character of the column

Example
SELECT * FROM STUDENT WHERE NAME LIKE 'A%';
┌────────┬────────┬─────────┬─────────┬───────┐
│ rollno │ Name │ Age │ City │ Marks │
├────────┼────────┼─────────┼─────────┼───────┤
│1 │ Aditya │ Patel │ Navsari │ 90.0 │
│2 │ Akash │ Gandhi │ Surat │ 80.0 │
│4 │ Aditi │ Patel │ Surat │ 70.0 │
│6 │ Ahana │ Rastogi │ Mumbai │ 95.0 │
│7 │ Aditya │ Shah │ Surat │ 93.0 │
└────────┴────────┴─────────┴─────────┴───────┘
SELECT * FROM emp_master WHERE first_name LIKE 's%';
┌────────┬────────────┬───────────┬────────┬──────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│ 10 │ Swayam │ Gandhi │ 20000 │ 16-5-2010 │ 1 │
└────────┴────────────┴───────────┴────────┴──────────
SELECT * FROM emp_master WHERE last_name LIKE '%n%';
┌────────┬────────────┬───────────┬────────┬──────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│1 │ Dev │ Rana │ 50000 │ 1-1-2010 │ 1 │
│3 │ Jhanvi │ Rana │ 70000 │ 3-10-2006 │ 1 │
│7 │ Niti │ Gandhi │ 80000 │ 18-8-2004 │ 3 │

Dr. Jyoti Rana


Naran Lala College, Navsari Page 11
│ 10 │ Swayam │ Gandhi │ 20000 │ 16-5-2010 │ 1 │
└────────┴────────────┴───────────┴────────┴─────────
SELECT * FROM emp_master WHERE first_name LIKE '%i';
┌────────┬────────────┬───────────┬────────┬─────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼─────────
│3 │ Jhanvi │ Rana │ 70000 │ 3-10-2006 │ 1 │
│4 │ Aditi │ Patel │ 65000 │ 9-11-1979 │ 2 │
│7 │ Niti │ Gandhi │ 80000 │ 18-8-2004 │ 3 │
└────────┴────────────┴───────────┴────────┴──────────
SELECT * FROM emp_master WHERE last_name LIKE 'Pat__';
┌────────┬────────────┬───────────┬────────┬─────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│2 │ Krishna │ Patel │ 30000 │ 5-4-2021 │ 1 │
│4 │ Aditi │ Patel │ 65000 │ 9-11-1979 │ 2 │
│5 │ Aryan │ Patel │ 55000 │ 11-12-2005 │ 2 │
└────────┴────────────┴───────────┴────────┴──────────

SQLite Between operator


It is used to get the rows or records within the range specified in query
statements.

Generally in SQLite, we can use Between operator with


Select,Update, Delete statements to get or update or delete records
based on the specified range values.

Syntax
Select * from tablename where column_name between value1 and value2;

Example

SELECT * FROM emp_master WHERE jdate BETWEEN '1-1-2010' AND '31-


12-2022';

┌────────┬────────────┬───────────┬────────┬────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼─────────
│1 │ Dev │ Rana │ 50000 │ 1-1-2010 │ 1 │

Dr. Jyoti Rana


Naran Lala College, Navsari Page 12
│3 │ Jhanvi │ Rana │ 70000 │ 3-10-2006 │ 1 │
│5 │ Aryan │ Patel │ 55000 │ 11-12-2005 │ 2 │
│7 │ Niti │ Gandhi │ 80000 │ 18-8-2004 │ 3 │
│8 │ Dev │ Shah │ 75000 │ 26-6-2009 │ 3 │
│9 │ Krishna │ Shah │ 60000 │ 1-5-2010 │ 2 │
│ 10 │ Swayam │ Gandhi │ 20000 │ 16-5-2010 │ 1 │
└────────┴────────────┴───────────┴────────┴─────────

select * from emp_master where emp_id between 5 and 10;


┌────────┬────────────┬───────────┬────────┬─────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼─────────
│5 │ Aryan │ Patel │ 55000 │ 11-12-2005 │ 2 │
│6 │ Ahana │ Rastogi │ 44000 │ 5-1-2003 │ 3 │
│7 │ Niti │ Gandhi │ 80000 │ 18-8-2004 │ 3 │
│8 │ Dev │ Shah │ 75000 │ 26-6-2009 │ 3 │
│9 │ Krishna │ Shah │ 60000 │ 1-5-2010 │ 2 │
│ 10 │ Swayam │ Gandhi │ 20000 │ 16-5-2010 │ 1 │
└────────┴────────────┴───────────┴────────┴──────────

SQLite Between with Not Operator


In SQLite, if we use Not with Between operator then it will return the
records which are not in the defined range.

select * from emp_master where emp_id not between 5 and 10;


┌────────┬────────────┬───────────┬────────┬──────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│1 │ Dev │ Rana │ 50000 │ 1-1-2010 │ 1 │
│2 │ Krishna │ Patel │ 30000 │ 5-4-2021 │ 1 │
│3 │ Jhanvi │ Rana │ 70000 │ 3-10-2006 │ 1 │
│4 │ Aditi │ Patel │ 65000 │ 9-11-1979 │ 2 │

SQLite IN Operator
In SQLite IN operator is used to determine whether the given value
matching with subquery returned result set values or list of given
values.

It is mainly used to check whether the given column value matching


with any of the value in the list of values or subquery values.
Dr. Jyoti Rana
Naran Lala College, Navsari Page 13
Syntax
Select * from tablename where column_name in(Value1,Value2,….,Valuen)
Or
Select * from tablename where column_name in(subquery)

Example

SELECT * FROM STUDENT WHERE Marks IN(85,90,95);


┌────────┬────────┬─────────┬─────────┬───────┐
│ rollno │ Name │ Surname │ City │ Marks │
├────────┼────────┼─────────┼─────────┼───────┤
│1 │ Aditya │ Patel │ Navsari │ 90.0 │
│3 │ Jiya │ Rana │ Valsad │ 85.0 │
│6 │ Ahana │ Rastogi │ Mumbai │ 95.0 │
└────────┴────────┴─────────┴─────────┴───────┘

SELECT name FROM vegetable WHERE color IN ('Orange','Red');


┌────────┐
│ name │
├────────┤
│ Carrot │
│ Tomato │
└────────┘

SQLite IN Operator with SubQuery


We can use IN operator with subquery as follows.

SELECT * from emp_master WHERE dept_id IN (SELECT dept_id


from dept_master WHERE dept_name= 'Admin');
┌────────┬────────────┬───────────┬────────┬──────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│1 │ Dev │ Rana │ 50000 │ 1-1-2010 │ 1 │
│2 │ Krishna │ Patel │ 30000 │ 5-4-2021 │ 1 │
│3 │ Jhanvi │ Rana │ 70000 │ 3-10-2006 │ 1 │
│ 10 │ Swayam │ Gandhi │ 20000 │ 16-5-2010 │ 1 │
└────────┴────────────┴───────────┴────────┴──────────

Dr. Jyoti Rana


Naran Lala College, Navsari Page 14
SELECT * FROM STUDENT WHERE Marks NOT IN(85,90,95);
┌────────┬──────────┬─────────┬───────┬───────┐
│ rollno │ Name │ Surname │ City │ Marks │
├────────┼──────────┼─────────┼───────┼───────┤
│2 │ Akash │ Gandhi │ Surat │ 80.0 │
│4 │ Aditi │ Patel │ Surat │ 70.0 │
│5 │ Devanshi │ Rana │ Vapi │ 82.0 │
│7 │ Aditya │ Shah │ Surat │ 93.0 │

SQLite Union operator


It is used to combine the result sets of 2 or more SELECT statements
and it removes duplicate rows between the various SELECT
statements.

SELECT dept_id FROM dept_master


UNION
SELECT dept_id FROM emp_master;

Union with order by

SELECT dept_id FROM dept_master


UNION
SELECT dept_id FROM emp_master order by dept_id;

Union All
It is used to combine the result sets of 2 or more SELECT statements
and it will return all the rows including duplicates.

SELECT dept_id FROM dept_master


UNION ALL
SELECT dept_id FROM emp_master;

Union All with order by


SELECT dept_id FROM dept_master
UNION ALL
SELECT dept_id FROM emp_master order by dept_id;

Dr. Jyoti Rana


Naran Lala College, Navsari Page 15
SQLite Intersect operator

It is useful to get only matching rows from two or more select statements.

SELECT dept_id FROM dept_master


INTERSECT
SELECT dept_id FROM emp_master;

SQLite Except operator


It will return all the records from a first select statement except
the records which exist in the second Select statement.

SELECT dept_id FROM dept_master


EXCEPT
SELECT dept_id FROM emp_master;

SQLite Limit clause


It is used to set a limit to the records returned by select statement.

SELECT first_name, last_name FROM emp_master LIMIT 5;

SQLite Limit Clause with Offset


In SQLite Limit clause the optional OFFSET specifies how many rows
to skip at the beginning of the result set.

SELECT first_name, last_name FROM emp_master LIMIT 4 OFFSET 1;

 SQLite NULL values


All relational databases will support a special value called NULL and it is
used to represent missing information. If table has Null value, then it will
display as blank.

SELECT * FROM student WHERE Marks = NULL;


SELECT * FROM student WHERE Marks is NULL;
SELECT * FROM student WHERE Marks IS NOT NULL;

Dr. Jyoti Rana


Naran Lala College, Navsari Page 16
 SQLite Group By Clause
It is used to aggregate data into a single row where the value of one or more specified
columns is repeated. This feature can be used to reduce the number of records to only
find unique values of a column.
select color from vegetable group by color;

SELECT first_name, SUM(salary) FROM emp_master GROUP BY


first_name;

SELECT first_name, SUM(salary), count(first_name) FROM


emp_master GROUP BY first_name;

SELECT first_name, COUNT(first_name) FROM emp_master


GROUP BY first_name HAVING COUNT(first_name) > 1;

SELECT min(e.salary), max(e.salary),d.dept_name FROM


emp_master e, dept_master d WHERE e.dept_id = d.dept_id group
by e.dept_id;

SELECT * FROM emp_master GROUP BY dept_id, first_name;

SQLite Order By Clause


It is used to sort column records either in ascending or descending
order.

SELECT * FROM emp_master ORDER BY SALARY DESC;

SELECT last_name, first_name FROM emp_master ORDER BY 2


DESC;

Dr. Jyoti Rana


Naran Lala College, Navsari Page 17

You might also like