Topics in this sectionDatabase Queries and Relationships2 topics
- Creating Queries
- Database Relationships and Normalization
Creating Queries in Databases
When you work with large amounts of data stored in tables, you often need to find only specific information rather than looking at everything at once. A query is a question you ask a database that tells it exactly what data you want to see. Queries let you retrieve, sort, and filter information from one or more tables, helping you make sense of raw data quickly.
Imagine you have a database of customers at a market stall in Juba. The database contains a table with columns for customer name, city, amount spent, and product purchased. A query allows you to ask questions like:
- "Show me only customers who live in Wau"
- "List all purchases sorted from highest to lowest amount"
- "Find every customer who bought sorghum and spent more than 5,000 SSP"
Instead of scrolling through hundreds of rows, a query extracts exactly what you need.
There are two main approaches to creating queries: using the query design view and writing SQL code directly.
Using Query Design View
Most database programs like Microsoft Access provide a visual query builder. You select the tables you need, choose the fields you want to display, and set criteria by filling in boxes. For example, to find customers in a specific city, you would:
- Add the customer table to the query design window
- Double-click the fields you want to see (name, city, amount)
- In the "Criteria" row under the city field, type "Wau"
The database automatically translates your visual choices into instructions it can run.
Using SQL
SQL stands for Structured Query Language. It is the standard language used to communicate with databases. Even when you use the visual query builder, the computer actually writes SQL behind the scenes. Learning SQL gives you more control and lets you write complex queries that visual builders cannot handle easily.
The most basic SQL command is the SELECT statement, which tells the database what to retrieve.
The SELECT statement is the foundation of SQL. Its basic structure looks like this:
SELECT field1, field2
FROM table_name
WHERE condition;
- SELECT lists the columns you want to see
- FROM tells which table holds the data
- WHERE sets the condition for filtering
For example, to find all customers in Malakal from a table called Customers, you would write:
SELECT Name, City, Amount
FROM Customers
WHERE City = 'Malakal';
This query displays only the name, city, and amount columns for rows where the city is Malakal.
Often you need information arranged in a particular order. The ORDER BY clause sorts your results. To see customers listed from the highest amount spent to the lowest, you would add:
SELECT Name, City, Amount
FROM Customers
ORDER BY Amount DESC;
The word DESC means descending (largest to smallest). Use ASC for ascending order, or omit it since ascending is the default.
The WHERE clause filters data based on conditions. You can use several operators:
- = equals (City = 'Juba')
- <> not equal (City <> 'Juba')
- > greater than (Amount > 5000)
- < less than (Amount < 1000)
- BETWEEN within a range (Amount BETWEEN 1000 AND 5000)
- LIKE pattern matching for text (City LIKE 'J%' finds cities starting with J)
To find customers who spent more than 3,000 SSP, write:
SELECT Name, Amount
FROM Customers
WHERE Amount > 3000;
Real databases often store related information in separate tables. For example, you might have a Customers table and an Orders table. To see which customer placed each order, you need to combine these tables using a join.
Inner Join
An inner join returns only the rows where there is a match in both tables. If a customer exists in the Customers table but has no orders in the Orders table, that customer will not appear in the results.
SELECT Customers.Name, Orders.OrderDate, Orders.Amount
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
This connects the two tables using the CustomerID field that appears in both.
Outer Join
An outer join returns all rows from one table and matching rows from the other. If you want to see every customer even those who have never placed an order, you would use a left outer join:
SELECT Customers.Name, Orders.OrderDate, Orders.Amount
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
A left outer join keeps all records from the left table (Customers) and includes matching records from the right table (Orders). A right outer join does the opposite.
Consider a school database in a county town with two tables:
Students (StudentID, Name, County, Age) Enrollments (EnrollmentID, StudentID, Subject, Grade)
To list all students and their grades, showing even those not yet enrolled in any subject:
SELECT Students.Name, Students.County, Enrollments.Subject, Enrollments.Grade
FROM Students
LEFT JOIN Enrollments ON Students.StudentID = Enrollments.StudentID;
This query displays every student from the Students table. Where a student has enrollments, their subject and grade appear alongside their name. Where a student has no enrollments, the subject and grade fields show as empty or null.
In a small clinic in Wau, health workers record patient visits in a database. They use queries to find all patients from a particular boma who are overdue for follow-up treatment, or to generate a list of medicines that stock levels below a certain threshold. A query can quickly show which medicines need reordering, helping the clinic manage limited supplies efficiently without manually checking every record.
Question
In a clinic database in Wau, you want to see only the names of patients who live in the city of Wau. Which SQL clause is used to filter this data?
Sign in to submit your answer and have it count toward your mastery.
Sign in to practiceTutor
Stuck? Ask me anything about this topic.
Sign in to ask the Sonza AI Tutor about this question.
Sign in to askDiscussions
No questions yet