SQLite

How to Select Two Columns in SQLite

Are you searching for an efficient and streamlined way to retrieve data from an SQLite database? One of the most fundamental operations in SQLite is the SELECT statement, which allows you to extract specific data from your database tables.

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:

SELECT column1, column2 FROM TABLE_NAME;

Let’s follow up with a simple example where we have first created a table student in SQLite using the following statement.

CREATE TABLE Students (
    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:

SELECT name, age FROM Students;

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.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.