UPI Study › Courses › Database Programming › Free lessons › Lesson 1.4 Free with a free account
Querying Databases: Definitions and Essential Tools
Database Programming · Chapter 1: Getting Started with Databases and SQL SetupSection 1.4 · lesson 4 of 768 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: DDL, DML, and DCL in MySQL: What They Do and When to Use Them →← Planning a Database Schema: Key Ideas and StepsNext lesson: DDL, DML, and DCL in MySQL: What They Do and When to Use Them →
A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →
In this lesson
Write basic SELECT queries and read the result sets they return.
- Define a query, a SQL statement, and a result set
- Use SELECT, FROM, and WHERE to choose columns and filter rows
- Distinguish quoted text literals from identifiers and interpret empty results
- Run saved .sql scripts with MySQL, SQL Server, and SQLite client tools
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (984 words). Read along, or search it.
Opening Question
Asking for Customers in Pune
Maya needs a list of customers in Pune. The stored records already exist; how does she ask for just the answer she needs? A query is a request for data. A SQL statement expresses that request in a form the database engine can interpret. A result set is a table-shaped answer, with named columns and zero or more rows. It is separate from the stored table.
Sample Data
The Lantern Books Customers Table
Our sample is the customers table at Lantern Books. Each record has an identifier, first and last names, city, and points. The supplied setup file creates those fictional records. We are using SQLite for the runnable demonstrations; the basic queries transfer to MySQL and SQL Server. Run the setup once, in a fresh practice folder. It creates the local shop database file; successful setup prints no rows. These are prepared records. Table creation comes later.
Select Everything
SELECT Star with ORDER BY
Select star from customers requests every column from that table. The star is shorthand for all columns. We add order by customer ID for a predictable display. Without an ordering clause, the engine does not promise a particular row order. Run the file through the SQLite shell, using header and column display options. Those options format the terminal answer; they do not change the query. The answer has six rows and five columns, matching the shared fictional sample.
Choosing Columns
Selecting First Name and City
Now select first name and city, from customers. Select names the result columns; commas separate their names. From names the source table. The source still contains all its original columns, even though this request asks for fewer. Run the statement. The answer becomes narrower; stored information remains intact. The result has six rows, with only first name and city. Reversing the selected column names would reverse their display order.
Filtering Rows
WHERE Keeps Rows That Test True
Add where city equals Pune. Where supplies a condition, which is a test applied to the candidate rows. Pune is a text literal, so it belongs inside single quotes. Keep rows only when the condition evaluates to true. Run the file. The logical reading model is from the source, through the filter, to the selected columns. Asha, Chen, and Farid remain. The result contains three rows and two columns; the stored customers table remains intact.
Logical Model
Reading Order Versus Engine Execution
The logical model helps you predict an answer, but it is not a promise about the engine's physical execution steps. The optimizer can choose indexes and other strategies, while preserving the meaning of the request. Uppercase keywords aid readability. Lowercase select has the same meaning; names and text comparisons depend on dialect and collation.
Numeric Filter
Filtering by Customer ID Four
A numeric condition needs no text quotes. Here, customer ID equals four, so the request targets one identified record. The selected columns are still first name and city. A filter may use a column that is absent from the result. Run the request, and inspect the returned values. The identifier selects Dara, whose city is Lagos. These ordinary select statements read the records, without changing them.
Empty Results
No Matching Rows Is Not an Error
What if no row meets the condition? This request asks for customers in Oslo, which is absent from our sample. Run it. SQLite's column display prints no result rows here, and no header; that is an empty answer, rather than an error. A graphical grid may still show column names, despite containing zero records.
Common Mistake
Unquoted Pune Becomes an Identifier
Here is a common slip: Pune appears without single quotes. The engine treats that unquoted word as an identifier. Our customers table has no column with that name. Run this faulty statement to see the actual SQLite message. The diagnostic says no such column, and names Pune. The wording is SQLite's; MySQL and SQL Server report their own diagnostics. Single quotes would express a text value, rather than an identifier.
Query Tools
Language Versus Interface
SQL is the language; the query tool is the interface. MySQL's mysql client and SQL Server's sqlcmd work in text terminals. Workbench provides a MySQL editor and result grid. On Windows, SSMS provides an object browser, editor, Execute action, results, and messages. Azure Data Studio retired in February, twenty twenty-six. Use a supported tool, such as Visual Studio Code with its MSSQL extension, instead of an old installation guide.
Saved Scripts
SQL Files, Comments, and Semicolons
A script is a plain-text file of saved statements, normally ending in .sql. Save it to repeat work and share the exact request. A double-dash comment explains the purpose without changing the result. In MySQL, follow the dashes with a space. End statements with semicolons. Missing separators, misspelled names, or a missing from clause can produce errors; inspect the actual message rather than guessing. In graphical tools, selected text changes what executes. Check the selection before expecting the whole saved file to run.
Running Scripts
Client Commands for Saved Files
For MySQL, choose shop with use shop, then run a query. The client can read a saved file with its source command. For SQL Server, sqlcmd accepts a database and an input file. Go on a line by itself submits a batch in Microsoft tools. GO and source are tool instructions, rather than portable SQL statements. SQLite opens shop.db through the shell command shown earlier. Workbench and SSMS also open script files. Use the command for the server and tool you actually installed.
Quick Check
Predict the Shape for Customer Six
Quick check: the request selects city and points, where customer ID equals six. Predict the result's shape, before running it. Read the choices, then pause, and predict.
Answer Explained
One Row and Two Columns
One row and two columns. The run returns Pune and ninety-five points, because customer ID six identifies Farid. Select controls the columns, while where limits the rows. The other choices confuse stored-table size with the shape requested by this statement. The result provides exactly the selected properties for the matching record, without changing that record.
Key Takeaways
Queries, Filters, and Result Sets
A query asks for data, and a result set is its table-shaped answer. Select chooses columns, from names a source, and where keeps qualifying rows. An empty answer can be valid. Use quoted text, exact object names, and semicolons. Read diagnostics separately from successful query results. Save repeatable scripts, and match the client instructions to the engine you are using.
About Database Programming at UPI Study
Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Querying Databases: Definitions and Essential Tools
What is the difference between a query and a result set?
Why does a text value in a WHERE clause need single quotes?
What tools can run SQL queries on MySQL and SQL Server?
Is a SQL query that returns no rows an error?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Database Programming.
76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →