Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Select Statement

SQLite Select Statement

SQLite's SELECT The statement is used to fetch data from a SQLite database table, returning the data in the form of a result table. These result tables are also called result sets.

Syntax

The basic syntax of the SQLite SELECT statement is as follows:


SELECT column1, column2, columnN FROM table_name;

Here, column1, column2... are the fields of the table whose values you want to fetch. If you want to fetch all available fields, you can use the following syntax:


SELECT * FROM table_name;

Example

Assume the COMPANY table has the following records:


ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

The following is an example that uses the SELECT statement to fetch and display all these records. Here, the first two commands are used to set properly formatted output.


sqlite>.header on
sqlite>.mode column
sqlite> SELECT * FROM COMPANY;

Finally, the following result will be obtained:


ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

If you only want to fetch specified fields from the COMPANY table, use the following query:


sqlite> SELECT ID, NAME, SALARY FROM COMPANY;

The above query will produce the following result:


ID          NAME        SALARY
----------  ----------  ----------
1           Paul        20000.0
2           Allen       15000.0
3           Teddy       20000.0
4           Mark        65000.0
5           David       85000.0
6           Kim         45000.0
7           James       10000.0

Setting the Width of Output Columns

Sometimes, due to the default width of the columns to be displayed, .mode columnthe output is truncated in this case. At this point, you can use the .width num, num.... command to set the width of the displayed columns, as shown below:


sqlite>.width 10, 20, 10
sqlite>SELECT * FROM COMPANY;

The above .width The command sets the width of the first column to 10, the second column to 20, and the third column to 10. Therefore the above SELECT statement will produce the following result:


ID          NAME                  AGE         ADDRESS     SALARY
----------  --------------------  ----------  ----------  ----------
1           Paul                  32          California  20000.0
2           Allen                 25          Texas       15000.0
3           Teddy                 23          Norway      20000.0
4           Mark                  25          Rich-Mond   65000.0
5           David                 27          Texas       85000.0
6           Kim                   22          South-Hall  45000.0
7           James                 24          Houston     10000.0

Schema Information

Because alldot commandsare only available at the SQLite prompt, so when you program with SQLite, you use the following SELECT statement with the sqlite_master table to list all the tables created in the database:


sqlite> SELECT tbl_name FROM sqlite_master WHERE type = 'table';

Assuming that the only COMPANY table already exists in testDB.db, the following result will be produced:


tbl_name
----------
COMPANY

You can list complete information about the COMPANY table, as shown below:


sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';

Assuming that the only COMPANY table already exists in testDB.db, the following result will be produced:


CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
)