Introduction
In this lab, you will learn how to query and filter data in a SQLite database. This hands-on lab is designed for beginners and will introduce you to essential SQL techniques. You'll learn how to filter data using the WHERE clause, match patterns with LIKE, sort results with ORDER BY, and limit the number of results with LIMIT. By the end of this lab, you'll be able to retrieve specific data from a database efficiently.
Connect to SQLite and View the Data
In this first step, you'll connect to a SQLite database named employees.db and view the data within the staff table. This allows you to understand the data you'll be working with.
Open a terminal in the LabEx VM environment. The default directory is
/home/labex/project.Connect to the
employees.dbdatabase using thesqlite3command-line tool:sqlite3 employees.dbThis command opens the SQLite shell, indicated by the
sqlite>prompt.View the data in the
stafftable. Execute the following SQL command:SELECT * FROM staff;This command selects all columns (
*) from thestafftable and displays the results. You should see the following output:1|Alice|HR 2|Bob|IT 3|Charlie|HR 4|David|IT 5|Eve|Marketing 6|Frank|ITThis output shows the
id,name, anddepartmentfor each employee in thestafftable.To exit the SQLite shell, type:
.quitIMPORTANT: Make sure
.quitbefore you click the "Continue" button. Otherwise, the step cannot be verified because the sqlite history will not be recorded.
Filter Data with the WHERE Clause
The WHERE clause allows you to filter data based on specific conditions. You can use it to retrieve only the rows that meet your criteria.
Connect to the
employees.dbdatabase:sqlite3 employees.dbTo retrieve only employees from the
HRdepartment, use the following SQL command:SELECT * FROM staff WHERE department = 'HR';This command selects all columns from the
stafftable, but only includes rows where thedepartmentcolumn is equal toHR. The output will be:1|Alice|HR 3|Charlie|HRNow, let's find employees with an
idgreater than 3:SELECT * FROM staff WHERE id > 3;This command selects all columns from the
stafftable, but only includes rows where theidcolumn is greater than 3. The output will be:4|David|IT 5|Eve|Marketing 6|Frank|ITExit the SQLite shell:
.quitIMPORTANT: Make sure
.quitbefore you click the "Continue" button. Otherwise, the step cannot be verified because the sqlite history will not be recorded.
Pattern Matching with the LIKE Operator
The LIKE operator is used for pattern matching. It's helpful when you want to find data that matches a specific pattern, rather than an exact value. You'll use wildcards like % (zero or more characters) and _ (single character).
Connect to the
employees.dbdatabase:sqlite3 employees.dbTo find employees whose names start with
A, use the following command:SELECT * FROM staff WHERE name LIKE 'A%';This command selects all columns from the
stafftable, but only includes rows where thenamecolumn starts withA. The%wildcard matches zero or more characters. The output will be:1|Alice|HRTo find employees whose names contain the letter
i, use:SELECT * FROM staff WHERE name LIKE '%i%';This command selects all columns from the
stafftable, but only includes rows where thenamecolumn contains the letteri. The output will be:1|Alice|HR 3|Charlie|HR 4|David|ITExit the SQLite shell:
.quit
Sort Data with the ORDER BY Clause
The ORDER BY clause allows you to sort the results of your query. You can sort in ascending (default) or descending order.
Connect to the
employees.dbdatabase:sqlite3 employees.dbTo sort the data alphabetically by
namein ascending order, use:SELECT * FROM staff ORDER BY name;This command selects all columns from the
stafftable and sorts the results by thenamecolumn in ascending order (A to Z). The output will be:1|Alice|HR 2|Bob|IT 3|Charlie|HR 4|David|IT 5|Eve|Marketing 6|Frank|ITTo sort by
namein descending order, use:SELECT * FROM staff ORDER BY name DESC;This command selects all columns from the
stafftable and sorts the results by thenamecolumn in descending order (Z to A). The output will be:6|Frank|IT 5|Eve|Marketing 4|David|IT 3|Charlie|HR 2|Bob|IT 1|Alice|HRExit the SQLite shell:
.quit
Limit Results with the LIMIT Clause
The LIMIT clause restricts the number of rows returned by a query. This is useful when you only need a subset of the data.
Connect to the
employees.dbdatabase:sqlite3 employees.dbTo retrieve only the first 3 rows from the
stafftable, use:SELECT * FROM staff LIMIT 3;This command selects all columns from the
stafftable and limits the results to the first 3 rows. The output will be:1|Alice|HR 2|Bob|IT 3|Charlie|HRCombine
LIMITwithORDER BYto get the first 2 employees sorted bynamein descending order:SELECT * FROM staff ORDER BY name DESC LIMIT 2;This command selects all columns from the
stafftable, sorts the results by thenamecolumn in descending order, and limits the results to the first 2 rows. The output will be:6|Frank|IT 5|Eve|MarketingExit the SQLite shell:
.quit
Summary
In this lab, you have learned how to query and filter data in a SQLite database. You connected to a database, filtered data using the WHERE clause, matched patterns with the LIKE operator, sorted results with ORDER BY, and limited the number of rows returned using LIMIT. These techniques provide you with the fundamental skills to retrieve and organize data effectively.


