Google News
logo
SQLite Interview Questions
SQLite is a relational database management system which is self-contained, server-less and need zero configuration.
* SQLite was designed by D. Richard Hipp for the purpose of no administration required for operating a program.
 
* SQLite was designed originally on August 2000. It is named SQLite because it is very light weight (less than 500Kb size) unlike other database management systems like SQL Server or Oracle.
There are lots of features which make SQLite very popular :
 
* SQlite is free of cost.
* SQLite is server-less.
* SQLite is flexible.
* SQLite doesn't need configuration.
* SQLite is cross-platform.
* SQLite is very light weight database.
* Data storing is very easy and efficient.
* SQlite is very easy to learn and use.
In SQLite, sqlite3 command is used to create database.
 
Syntax :

Sqlite3 database_name.db

The standard SQLite commands interact with relational databases are similar to SQL. They are :
 
SELECT
CREATE
INSERT
UPDATE
DROP
DELETE

Based on their operational nature these commands can be classified.
The transaction is referred as a unit of work that is performed against a database. It is the propagation of one or more changes to the database. Properties of transactions are determined by ACID.
 
* Atomicity : It ensures that all work unit are successfully completed
* Consistency : It ensures that the database changes states upon a successfully committed transaction
* Isolation : It enables transactions to operate independently of and transparent to each other
* Durability : It ensures that the result or effect of a committed transaction persists in case of a system failure
SQLite works well with 
 
* Embedded devices and the internet of things
* Application file format
* Data Analysis
* Websites
* Cache for enterprise data
* Server side database
* File archives
* Internal or temporary databases
* Replacement for ad hoc disk files
* Experimental SQL language extensions
* Stand-in for an enterprise database during demos or testing
SQL :
* SQL is a Structured Query Language
* SQL support stored procedures
* SQL is server based

SQLite :
*
SQLite is a powerful, embedded relational database management system mostly used in mobile devices for data storage
* SQLite does not support stored procedures
* SQLite is file based
SQLite storage classes include
 
* Null : The value is a NULL value
* Integer : The value is a signed integer (1,2,3, etc.)
* Real : The value is a floating point value, stored as an 8 byte IEEE floating point number
* Text : The value is a text string, stored using the database encoding ( UTF-8, UTF-16BE)
* BLOB (Binary Large Object) : The value is a blob of data, exactly stored as it was input
SQLite can be used in following conditions
 
* Embedded applications : Does not require expansion like mobile applications or games
* Disk assess replacement : Application that require to write or read files to disk directly
* Testing : When testing business application logic
 
When not to use SQLite
 
* Multi-user applications : Where multiple client needs to access and use same database
* Applications requiring high write volumes : It enables you to use only one single write operation to take place at any given time
To recover the information you can use your backup copy of your database file, but if you do not have a backup copy, then recovery is impossible. SQLite uses SQLITE SECURE DELETE option which overwrites all deleted content with zeroes.
CREATE TABLE statement is used to create a table in SQLite database. You have to define the columns and data types of each column while creating the table.
 
Syntax :
CREATE TABLE database_name.table_name(    
column1 datatype  PRIMARY KEY(one or more columns),    
column2 datatype,    
column3 datatype,    
columnN datatype,    
);
For autoincrement, you have to declare a column of the table to be INTEGER PRIMARY KEY, then whenever you insert a NULL into that column of the table, the NULL is automatically converted into an integer which is one greater than the largest value of that column over all other rows in the table, or 1 if the table is empty.
INSERT INTO statement is used to insert data in a table in SQLite database. There are two ways to insert data in table :
 
Method1 :
 
Syntax :
INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]      
VALUES (value1, value2, value3,...valueN);  
 
Method2 :
 
Syntax :
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);

 

The SELECT command is used to retrieve data from SQLite table. If you want to retrieve all columns from the table use SELECT * otherwise use the specific column's name separated by commas.
 
Syntax :
SELECT * FROM table_name;     
Or   
SELECT column1, column2, columnN FROM table_name;

 

The UPDATE query is used to modify the existing records in the SQLite table. You have to use the WHERE clause to modify a specific row otherwise all rows will be updated.
 
Syntax :
UPDATE table_name    
SET column1 = value1, column2 = value2...., columnN = valueN    
WHERE [condition];

 

In SQLite, DELETE command is used to delete the existing records from a table. You should use the WHERE clause to choose the specific row otherwise all rows will be deleted.
 
Syntax :
DELETE FROM table_name WHERE [conditions....................];

 

 

LIMIT clause is used with SELECT statement when we want a limited number of fetched records.
 
Syntax :
SELECT column1, column2, columnN
FROM table_name    
LIMIT [no of rows]
The ORDER BY clause is used to sort the fetched data in a specific order either ascending or descending.
 
Syntax :
SELECT column-list     
FROM table_name     
[WHERE condition]     
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
The core public domain SQLite source code is not described by any ECCN. Hence, the ECCN should be reported as EAR99. But if you are adding new code or linking SQLite with the application, then it might change the EECN number.
SQLite is recommended to be used because of following advantages :

* SQLite supports almost all Operating Systems.
* It is free of cost and flexible.
* It is very compact less than 400KiB.
* It requires no setup or administration as it comes with zero-configuration.
* No separate server processor system is required to operate.
* An SQLite database can be stored in a single cross-platform disk file.
* It is self-contained that means no external dependencies.
* It provides easy access to API as it is written in ANSI-C.
The SQLite aggregate functions are the functions where values of multiple rows and columns are grouped as input and form a single value as output.

Below is the list of SQLite aggregate functions :

* SQLite MIN function : It is used to select a minimum value for a column.
* SQLite MAX function : It is used to select a maximum value for a column.
* SQLite SUM function : It is used to select the total for a numeric column.
* SQLite AVG function : It is used to select the average value for a table column.
* SQLite COUNT function : It is used to count the number of rows in a database table.
* SQLite UPPER function : It is used to convert a string into upper-case letters.
* SQLite LOWER function : It is used to convert a string into lower-case letters.
* SQLite LENGTH function : It is used get the length of a string.
The transaction is actually referred to the unit of work that is performed against a database. The SQLite transactions are the propagation of one or more changes to the database.

The properties of transactions are determined by ACID compliance.

* Atomicity : This property ensures that the entire work unit is completed successfully.

* Consistency :
This property ensures that the database changes its state upon a successfully committed transaction.

* Isolation :
This property ensures that the transactions are operated independently and are transparent to each other.

* Durability :
This property ensures that the result or effect of a committed transaction will persist in case of system failure.
SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to store a 500-million character string there. And it will keep all 500-million characters intact. Your content is never truncated. SQLite understands the column type of "VARCHAR(N)" to be the same as "TEXT", regardless of the value of N.
SQLite allows you to store BLOB data in any column, even columns that are declared to hold some other type. BLOBs can even be used as PRIMARY KEYs.
SQLite GROUP BY clause is used to collect the same elements into a group. It is used with SELECT statement.
 
Syntax :
SELECT column-list    
FROM table_name    
WHERE [ conditions ]    
GROUP BY column1, column2....columnN    
ORDER BY column1, column2....columnN 
The DISTINCT clause is always used with SELECT statement. It is used to retrieve only unique records and restrict the duplicate entries.
 
It is used when the table has multiple duplicate records.
 
Syntax :
SELECT DISTINCT column1, column2,.....columnN     
FROM table_name    
WHERE [condition]
SQLite UNION Operator is used to combine the result set of two or more tables using SELECT statement. Both the tables must have same number of fields in result table.
 
Syntax :
SELECT expression1, expression2, ... expression_n    
FROM tables    
[WHERE conditions]    
UNION    
SELECT expression1, expression2, ... expression_n    
FROM tables    
[WHERE conditions];
The UNION ALL operator is used to combine the result of two or more tables using SELECT statement. The unique difference between UNION and UNION ALL operator is that UNION operator ignores the duplicate entries while combining the results while UNION ALL doesn't ignore duplicate values.
 
Syntax :
SELECT expression1, expression2, ... expression_n    
FROM tables    
[WHERE conditions]    
UNION ALL    
SELECT expression1, expression2, ... expression_n    
FROM tables    
[WHERE conditions];
SQLite JOIN clause is used to combine two or more tables in a database. It combines the table by using the common values of the both table.
 
There are mainly three types of JOINS supported in SQlite :
 
* SQLite INNER JOIN
* SQLite OUTER JOIN
* SQLite CROSS JOIN
SQLite INNER JOIN is simplest and most common join. It combines all rows from both tables where the condition is satisfied.
 
Syntax :
SELECT ... FROM table1 [INNER] JOIN table2 ON conditional_expression ...
There are three types of OUTER JOINS :
 
* Left outer join
* Right outer join
* Full outer join

But SQLite only supports left outer join. The SQLite left outer join returns all rows from left table and only those rows from the right table where the join condition is satisfied.
 
Syntax :
SELECT ... FROM table1 LEFT OUTER JOIN table2 ON conditional_expression
The SQLite Cross join is used to match every rows of the first table with every rows of the second table. If the first table contains x columns and second table contains y columns then the resultant Cross join table will contain the x*y columns.
 
Syntax :
SELECT ... FROM table1 CROSS JOIN table2
SQLite date and time () functions are used to retrieve current date and time and also do calculations on the dates and time.
 
There are mainly 6 types of date and time () function in SQLite:
 
* SQLite date() Function
* SQLite datetime() Function
* SQLite julianday() Function
* SQLite now() Function
* SQLite strftime() Function
* SQLite time() Function
The SQLite date() function is used to fetch the date and show it in 'YYYY-MM-DD' format.
 
Syntax :
date(timestring, [ modifier1, modifier2, ... modifier_n ] )
The SQLite datetime() function is used to retrieve current date and time in 'YYYY-MM-DD HH:MM:SS' format.
 
Syntax :
datetime(timestring, [ modifier1, modifier2, ... modifier_n ] )
SQLite MIN aggregate function is used to retrieve the minimum value of the expression.
 
Syntax :
SELECT MIN(aggregate_expression)    
FROM tables    
[WHERE conditions];
SQLite MAX aggregate function is used to fetch the maximum value of an expression.
 
Syntax :
SELECT MAX(aggregate_expression)    
FROM tables    
[WHERE conditions];
The SQLite AVG function returns the average value of the expression.
 
Syntax :
SELECT AVG(aggregate_expression)    
FROM tables    
[WHERE conditions];
The SQLite COUNT function is used to retrieve total count of an expression.
 
Syntax :
SELECT COUNT(aggregate_expression)    
FROM tables    
[WHERE conditions]; 
The SQLite SUM aggregate function is used to get the total summed value of an expression.
 
Syntax :
SELECT SUM(aggregate_expression)    
FROM tables    
[WHERE conditions];
SQLite strftime() function is used to fetch date and time and also perform time and date calculation.
 
Syntax :
strftime(format, timestring [, modifier1, modifier2, ... modifier_n ] )
SQLite time() function is used to fetch current time in 'HH-MM-SS' format.
 
Syntax :
time(timestring, [ modifier1, modifier2, ... modifier_n ] )
SQLite Aggregate functions are the functions where values of multiple rows are grouped together as input on certain criteria and form a single value as output. Following is a list of some SQLite Aggregate function :
 
* SQLite MIN Function
* SQLite MAX Function
* SQLite AVG Function
* SQLite COUNT Function
* SQLite SUM Function
* SQLite RANDOM Function
* SQLite ABS Function
* SQLite UPPER Function
* SQLite LOWER Function
* SQLite LENGTH Function
* SQLite sqlite_version Function