Searching and Queries in a Database
We learn how to write a query to find the records we need in a database, using conditions and sorting.
All topics (6 of 11)
Chapter 1. Spreadsheets
Chapter 2. Databases
Chapter 3. Computer Networks and the Web
Chapter 4. Number Systems
A database may contain thousands of records, and the one you need cannot be found by hand. A query is a command to select from the database the records that meet a certain condition. The result of a query is also shown as a table, but it contains only the records and the fields we need.
The core of a query is the condition. In a “Library” database, for example, we can write conditions such as “year of publication greater than 2015”, “author = Navoi” or “number of copies = 0”. Conditions are joined with “AND” and “OR”: with “AND” both conditions must hold, with “OR” one is enough. The comparison signs are =, <> (not equal), <, >, <= and >=.
A query can also sort its result, for example by listing books in descending order of publication year. We also choose which fields to display: if we need only the title and author, we leave the other fields out. In programs such as Access and LibreOffice Base, a query can be built in “Design” view as a table, with no programming needed.
Key takeaways
- A query selects the records that meet a condition from the database.
- Conditions are written with
=,<>,<,>,<=,>=. - With “AND” both conditions hold, with “OR” one is enough.
- A query lets you choose the fields and the sort order.
Practice tasks
- Create a “Books” table (title, author, year, copies) with 8 records and write a query with the condition “year > 2010”.
- Decide which records meet the condition “author = Navoi AND year < 2000”. How does the result change if you replace “AND” with “OR”?
- Describe a query that shows only the book title and author, sorted by year of publication (which fields, which condition and order).
Review questions
Test yourself: think first, then reveal the answer.
1What is a query for?Show answerHide answer
To quickly select the records that meet a condition from a large database.2How do “AND” and “OR” differ?Show answerHide answer
With “AND” both conditions must be true, with “OR” one is enough.
0 of 11 topics completed
Sign in to mark