Filter SQLite Data for IT Department

SQLiteBeginner
Practice Now

Introduction

In this challenge, you'll be working with an SQLite database named employees.db to filter employee data. The goal is to query the database and extract the names of all employees belonging to the IT department.

You'll need to connect to the database using the sqlite3 command, write a SELECT statement with a WHERE clause to filter by department, and then manually copy the output of your query and save it to a file named result.txt in the /home/labex/project/ directory. The result.txt file should contain a list of IT department employee names, one name per line.

Filter SQLite Data for IT Department

This challenge tests your ability to query specific data from an SQLite database, focusing on filtering employees by department.

Tasks

  • Write an SQL query to select the names of all employees in the IT department from the staff table.
  • Copy the output of your query and save it to a file named result.txt in the /home/labex/project/ directory.

Requirements

  1. Connect to the SQLite database named employees.db located in the /home/labex/project/ directory using the sqlite3 command.
  2. Write a SELECT statement to retrieve the name column.
  3. Use a WHERE clause to filter the results based on the department column.
  4. Execute the query, copy the results, and save them to /home/labex/project/result.txt using a text editor or the echo command.
  5. Your SQL query should be placed directly in the sqlite3 shell, without creating any additional SQL files.

Examples

Executing the correct query and saving the output should result in a result.txt file with the following content:

Bob
David
Frank

Hints

  • Remember to specify the database file path correctly.

  • You can copy the output from the terminal and use nano or another text editor to save it to result.txt.

  • Alternatively, you can use the echo command with appropriate redirection to save the output:

    echo -e "XXX" > /home/labex/project/result.txt
  • Ensure that your WHERE clause accurately filters for the IT department.

✨ Check Solution and Practice

Summary

In this challenge, the goal is to query an SQLite database named employees.db to retrieve the names of all employees belonging to the IT department. This involves connecting to the database using the sqlite3 command, writing a SELECT statement to retrieve the name column, and using a WHERE clause to filter the results based on the department column.

The key learning points include constructing an accurate WHERE clause to filter data based on a specific department value, and then manually copying and saving the query results to a specified file. The setup involves installing SQLite and creating the database and table with sample data.