SQLite Tutorial

  • Home
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • Interfaces
    • SQLite Java
    • SQLite Node.js
    • SQLite PHP
    • SQLite Python
  • Try It
Home / SQLite Cheat Sheet

SQLite Cheat Sheet

SQLite cheat sheet lists the most common SQLite statements that help you work with SQLite more quickly and effectively.

Managing databases

Attach another database to the current database connection:

1
ATTACH DATABASE file_name AS database_name;

Optimize the database:

1
VACUUM

Managing Tables

Create a new table:

1
2
3
4
5
6
CREATE TABLE [IF NOT EXISTS] table(
   primary_key INTEGER PRIMARY KEY,
   column_name type NOT NULL,
   column_name type NULL,
   ...
);

Rename a table:

1
ALTER TABLE table_name RENAME TO new_name;

Add a new column to a table:

1
ALTER TABLE table ADD COLUMN column_definition;

Drop an existing column in a table:

1
ALTER TABLE table DROP COLUMN column_name;

Drop a table and its data:

1
DROP TABLE [IF EXISTS] table_name;

Managing indexes

Creating an index

1
2
CREATE [UNIQUE] INDEX index_name
ON table_name (c1,c2,...)

Delete an index:

1
DROP INDEX index_name;

Create an expression index:

1
CREATE INDEX index_name ON table_name(expression);

Querying Data

Query all data from a table

1
SELECT * FROM table_name;

Query data from the specified column of a table:

1
2
SELECT c1, c2
FROM table_name;

Query unique rows

1
2
SELECT DISTINCT (c1)
FROM table_name;

Query rows that match a condition using a WHERE clause.

1
2
3
SELECT *
FROM table_name
WHERE condition;

Rename column in the query’s output:

1
2
SELECT c1 AS new_name
FROM table_name;

Query data from multiple tables using inner join, left join

1
2
3
SELECT *
FROM table_name_1
INNER JOIN table_name_2 ON condition;

1
2
3
SELECT *
FROM table_name_1
LEFT JOIN table_name_2 ON condition;

Count rows returned by a query:

1
2
SELECT COUNT (*)
FROM table_name;

Sort rows using ORDER BY clause:

1
2
3
SELECT c1, c2
FROM table_name
ORDER BY c1 ASC [DESC], c2 ASC [DESC],...;

Group rows using GROUP BY clause.

1
2
3
SELECT *
FROM table_name
GROUP BY c1, c2, ...;

Filter group of rows using HAVING clause.

1
2
3
4
SELECT c1, aggregate(c2)
FROM table_name
GROUP BY c1
HAVING condition;

Changing Data

Insert a row into a table:

1
2
INSERT INTO table_name(column1,column2,...)
VALUES(value_1,value_2,...);

Insert multiple rows into a table in a single statement:

1
2
3
4
INSERT INTO table_name(column1,column2,...)
VALUES(value_1,value_2,...),
      (value_1,value_2,...),
      (value_1,value_2,...)...

Update all rows in a table:

1
2
3
UPDATE table_name
SET c1 = v1,
    ...

Update rows that match with a condition:

1
2
3
4
UPDATE table_name
SET c1 = v1,
    ...
WHERE condition;

Delete all rows in a table

1
DELETE FROM table;

Delete rows specified by a condition:

1
2
DELETE FROM table
WHERE condition;

Search

Search using LIKE operator:

1
2
SELECT * FROM table
WHERE column LIKE '%value%'

Search using full-text search:

1
2
3
SELECT *
FROM table
WHERE table MATCH 'search_query';

  • Was this tutorial helpful ?
  • YesNo

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite GLOB
  • SQLite Left Join
  • SQLite Inner Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite Rename Column
  • SQLite DROP VIEW
  • SQLite Window Frame
  • SQLite CUME_DIST
  • SQLite PERCENT_RANK
  • SQLite DENSE_RANK
  • SQLite NTILE
  • SQLite NTH_VALUE

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2019 SQLite Tutorial. All rights Reserved.

⤒