BCA
SQL laboratory
Study map: Original subject-specific business–computing teaching chapter
SQL expresses operations on relational data. A table contains rows with defined columns; a key identifies a row; a relationship connects rows representing different objects. In a teaching order table, an order identifier can be unique and quantity and price can have constraints. A database can reject some invalid states, but it cannot determine every business truth. A positive quantity might still represent an order the customer never authorised.
Foundation: ask precise questions of related records
SQL expresses operations on relational data. A table contains rows with defined columns; a key identifies a row; a relationship connects rows representing different objects. In a teaching order table, an order identifier can be unique and quantity and price can have constraints. A database can reject some invalid states, but it cannot determine every business truth. A positive quantity might still represent an order the customer never authorised.
Begin with a clear grain: one row represents one accepted order line in this fixture. The report calculates quantity multiplied by unit price and sums accepted lines. It does not calculate profit, tax liability or actual cash receipts. Those questions require additional facts and definitions. A learner should name the measure before writing the query.
The local workshop creates SQLite tables in memory and inserts accepted synthetic records. It uses no external server or account. Read the schema and predict the number of rows before running the program. Then compare the result with the Python calculation. Agreement is a useful reconciliation, although two implementations can share the same mistaken specification.
Undergraduate: constraints, grouping and parameter binding
A primary key prevents duplicate identifiers within its table. A CHECK constraint can enforce a condition such as positive quantity. A NOT NULL constraint prevents missing values in required columns. These controls complement input validation. They do not eliminate the need to explain rejected records and govern corrections. In the lab, first classify each input and only then insert accepted records.
An aggregate such as SUM calculates over qualifying rows. GROUP BY separates results by a stated dimension, such as department. If one department has no accepted records, it may be absent rather than represented as zero; decide which meaning the report requires. Order the output explicitly when presenting results so the learner does not rely on incidental row order.
Bind values as query parameters instead of concatenating user input into SQL text. This separates values from SQL syntax. It does not make an unauthorised operation authorised. If a script is allowed to delete every record, careful parameter binding does not change that privilege. Explain security as several controls serving different purposes.
Postgraduate: joins and transaction boundaries
A join combines related rows. A one-to-many relationship can multiply an order row if it is joined to several events. Summing the order amount after that join may overstate value. Predict join cardinality with a tiny example: an order worth INR 100 and two events can produce two rows each showing INR 100. Aggregate events appropriately or calculate order value at its own grain before combining results.
A transaction groups changes under a defined boundary. If a required insert fails, decide whether the whole batch should roll back or whether valid rows may commit separately. Both policies can be reasonable for different work; they produce different recovery responsibilities. Never advertise a local transaction as a guarantee over an unrelated email, payment or remote system.
Use a grain–key–constraint–query–reconcile framework. Define row meaning, identity and relationships; enforce valid states; write the question; compare the result with independent expectations. In departmental work, finance can validate the value definition, operations the quantity and sales the customer intent. SQL supplies a reproducible calculation while humans validate its meaning.
Research: examine correctness under changing data
A study can compare reports before and after schema constraints are introduced. Define what counts as an invalid record and whether detection changed. Fewer stored invalid rows may mean better prevention, but increased rejection may also delay legitimate work. Examine correction and service outcomes alongside database integrity.
Create adversarial synthetic cases: missing keys, repeated identifiers, an empty table, an order with no events and an order with several events. Hand-calculate the expected results. Keep sensitive real records out of a public exercise. Explain why an empty aggregate may require explicit interpretation rather than being carelessly replaced by a plausible number.
Submit the schema, parameterised inserts, grouped query, reconciliation and correction policy. The in-memory database disappears when the script ends; durable storage, backup and multi-user access are separate exercises. Who owns a definition, whose key is used and which relationship changes the report are essential questions for trustworthy SQL practice.
Study questions and answers
What is the grain of the workshop order table?
One accepted order line per unique identifier. The report sums line value at that grain; it does not contain profit, tax or cash-receipt facts. Indicative marking: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
An INR 100 order joins to two events. What can a naive sum show?
Two joined rows each repeat INR 100, so summing produces INR 200. Aggregate at the order grain or handle event aggregation before calculating order totals. Indicative marking: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
Does parameter binding authorise a database change?
No. Binding separates data values from SQL syntax. Identity, permissions and legitimate business authority require separate controls. Indicative marking: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
How would you evaluate a new constraint?
Define invalid states consistently; examine prevention, rejection, correction delay and service outcomes. Account for changed detection and test legitimate boundary cases. Indicative marking: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
Extended practice questions
What is the grain of the workshop order table?
One accepted order line per unique identifier. The report sums line value at that grain; it does not contain profit, tax or cash-receipt facts.
Marking guide: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
Marks: 10
An INR 100 order joins to two events. What can a naive sum show?
Two joined rows each repeat INR 100, so summing produces INR 200. Aggregate at the order grain or handle event aggregation before calculating order totals.
Marking guide: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
Marks: 10
Does parameter binding authorise a database change?
No. Binding separates data values from SQL syntax. Identity, permissions and legitimate business authority require separate controls.
Marking guide: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
Marks: 10
How would you evaluate a new constraint?
Define invalid states consistently; examine prevention, rejection, correction delay and service outcomes. Account for changed detection and test legitimate boundary cases.
Marking guide: Correct concept or result 3; explanation 3; relevant case application 2; boundary or limitation 2.
Marks: 10
A revenue query joins orders to items and repeats each order total for every item. What should the learner inspect?
Identify the intended counting grain and the relationship between orders and item rows. If the measure is order revenue, aggregate one appropriate total per order rather than summing duplicated order totals after the join. If the measure is line revenue, calculate from the relevant item values instead. Do not use DISTINCT on amounts as a universal repair because two different orders may have the same total. Trace a small synthetic table by hand and compare it with the query output. Finance defines the measure; the developer makes the query implement that definition.
Marking guide: Order versus line grain; duplicated totals; DISTINCT amount trap; hand trace; measure ownership. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
Compare WHERE and HAVING in a grouped reporting query.
WHERE filters relevant input rows before the grouping result is formed. HAVING filters groups according to the applicable group condition, commonly involving an aggregate. For example, a date condition can restrict sales rows while a total threshold selects qualifying customer groups. Define whether rejected or cancelled rows belong in the input before summing. The exact query must match the intended business population, not merely run without an error. A grouped result can still be misleading when joins duplicate records or missing values are ignored without explanation. Test the query with a small expected-output fixture.
Marking guide: Row versus group filter; example; population; join/missing limits; expected fixture. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
Three synthetic orders total ₹1,000, ₹2,000 and ₹3,000. Each has two item rows. Calculate correct order revenue and the incorrect sum of order totals after the item join.
Correct order revenue is ₹1,000 + ₹2,000 + ₹3,000 = ₹6,000. The two item rows repeat every order total twice, so the incorrect joined sum is ₹12,000. The error is ₹6,000, or a 100% overstatement relative to correct revenue. This assumes the supplied order totals define the measure and all orders belong in its population. Aggregating distinct order identities is different from discarding repeated monetary values. Use a fixture containing equal-value orders too, so an accidental DISTINCT amount workaround does not appear valid simply because these three totals differ.
Marking guide: ₹6,000/₹12,000; 100% overstatement; order identities versus amounts; equal-value fixture. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
Specify an SQL reporting test fixture with meaningful failure cases.
Create synthetic orders with multiple items, two orders of equal value, a cancellation and a missing relationship. State the expected population, grain and totals before writing the query. Test row and group filters, inspect join effects and explain how missing or invalid relationships are handled. Reconcile the result with a hand-calculated table. Keep schema assumptions visible, including whether constraints are enforced in the chosen environment. Passing this fixture demonstrates the specified query behaviour; it is not proof that every production data source or future business definition has been validated.
Marking guide: Varied meaningful rows; expected population/grain; join/filter/missing handling; enforcement; scope. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
Revision cards
Why can a join unexpectedly increase the number of rows?
When multiple matches exist, a join combines matching pairs rather than assuming one row per entity. Inspect key uniqueness and relationship cardinality. Applying DISTINCT after the join may conceal the modelling problem while also removing legitimate observations.
A revenue query joins orders to items and repeats each order total for every item. What should the learner inspect?
Fix the counting grain, not just the displayed duplicates. DISTINCT on amounts can erase different equal-value orders; use the intended order or line definition.
Compare WHERE and HAVING in a grouped reporting query.
WHERE selects input rows; HAVING selects resulting groups. The query still needs correct population, joins and missing-value treatment.
Three synthetic orders total ₹1,000, ₹2,000 and ₹3,000. Each has two item rows. Calculate correct order revenue and the incorrect sum of order totals after the item join.
Correct ₹6,000; incorrect joined sum ₹12,000, a 100% overstatement. Preserve distinct order identities, not merely distinct monetary amounts.
Specify an SQL reporting test fixture with meaningful failure cases.
Use multi-item and equal-value orders, cancellation and missing links. Hand-calculate expected results and check join grain and filters before trusting the query.
What does NULL mean for an ordinary SQL equality comparison?
It represents missing or unknown information rather than a normal value equal to another NULL. Use appropriate null tests and consider three-valued logic. Replacing every NULL with zero changes meaning and may create false quantities or matches.
Why test a transaction’s rollback behaviour?
A transaction groups operations under database guarantees, but the intended business changes and failure path must be verified. Inspect the resulting state. Database rollback does not necessarily undo emails, remote API actions or other work performed outside the transaction.
How can GROUP BY change the unit of analysis?
It aggregates rows into groups defined by the chosen keys. The resulting row represents a group rather than an original event. Check the grouping and denominator so a customer-level average is not confused with a transaction-level average.
Why examine an execution plan instead of guessing at query speed?
The plan shows the database’s chosen access and processing strategy under its current information. Use representative inputs and measurements. An index can help some queries but adds costs and does not guarantee improvement for every workload.
Practice task
Complete computing-workshop.html: predict category counts and totals, run the script and tests, then submit the six portfolio deliverables.
Sources and further reading
- Computing for business workshop
Type: Original practice
Use: Predict, run, test and reconcile synthetic orders. - Python tutorial — for learners with some programming familiarity
Type: Free official learning documentation
Use: Use after the Library foundation: functions, control flow and errors. - Python collections reference
Type: Free official reference
Use: Inspect deque and mapping behaviour; not a complete algorithms course. - Python sqlite3 reference
Type: Free official reference
Use: Study binding and transaction controls for the local database. - GitHub Actions concepts
Type: Free official documentation
Use: Learn workflow vocabulary; hosted execution pricing is not evaluated here.