In this tutorial, you will learn a common scenario, that is selecting two columns from an SQLite table.
Before moving towards selecting the two columns in SQLite, it is better to gain some understanding of the SELECT statement.
What Does the SELECT Statement Do?
Tables are used to hold data in the relational database management system SQLite. Each table has rows and columns that, respectively, stand in for data entities and characteristics. In SQLite, the SELECT statement must be used to select data from a table. You may define the columns you wish to obtain from the table using the SELECT query.
How to Select Two Columns in SQLite?
In order to select two columns in SQLite, you need to use the SELECT statement followed by the names of the columns separated by a comma.
The basic syntax is:
Let’s follow up with a simple example where we have first created a table student in SQLite using the following statement.
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER
);
INSERT INTO Students (name, age) VALUES ('Leonard', 21);
INSERT INTO Students (name, age) VALUES ('Penny', 20);
INSERT INTO Students (name, age) VALUES ('Amy', 18);
INSERT INTO Students (name, age) VALUES ('Max', 22);
INSERT INTO Students (name, age) VALUES ('Howard', 20);
INSERT INTO Students (name, age) VALUES ('Caroline', 22);
INSERT INTO Students (name, age) VALUES ('Earl', 18);
The CREATE TABLE command creates a table called Students with three columns: id of type INTEGER, which serves as the primary key and has the AUTOINCREMENT attribute, name of type TEXT, and age of type INTEGER.
These instructions fill the Students table with sample data. The columns are listed clearly in each INSERT statement, followed by the relevant values for each column. Each student is given an individual identification number (or “id”) along with their name and age in this manner.
After that, we select the name and age columns from the Students table by writing the following SQL statement:
This statement will return all the rows from the Students table, with only the name and age columns being selected. In this manner, you may extract two columns from an SQLite table using the SELECT query.
Conclusion
Mastering the SELECT statement in SQLite is crucial for efficient data retrieval from your database. This tutorial has focused on selecting two columns from an SQLite table, providing you with a clear and straightforward approach. By understanding the purpose of the SELECT statement and its role in retrieving data, you can enhance your SQLite database management skills.