{"id":3949,"date":"2026-11-18T11:53:27","date_gmt":"2026-11-18T08:53:27","guid":{"rendered":"https:\/\/niteducation.com\/resources\/?p=3949"},"modified":"2026-11-18T11:53:31","modified_gmt":"2026-11-18T08:53:31","slug":"sql-joins-explained","status":"publish","type":"post","link":"https:\/\/niteducation.com\/resources\/sql-joins-explained\/","title":{"rendered":"SQL JOINs Explained: Build a Sales Report Step by Step"},"content":{"rendered":"\n<article class=\"nit-article nit-article--software\" aria-label=\"SQL JOINs explained with sales reporting example\">\n  <div class=\"nit-hero nit-hero--software nit-hero--with-answer\">\n    <div class=\"nit-software-hero__grid\">\n      <div class=\"nit-software-hero__copy\">\n        <p class=\"nit-hero__eyebrow\">NIT ICT LESSON \u00b7 SQL \u00b7 BEGINNER TO PRACTICAL<\/p>\n        <p class=\"nit-hero__title\">SQL JOINs Explained: <span>Build a Sales Report Step by Step<\/span><\/p>\n        <p class=\"nit-hero__summary\">Learn INNER, LEFT, RIGHT and FULL OUTER JOINs using four linked sales tables. Run each query, see the result, find wrong totals and verify a five-day report.<\/p>\n        <div class=\"nit-hero__actions\">\n          <a class=\"nit-button nit-button--primary\" href=\"#build-sales-report\">Build the report<\/a>\n          <a class=\"nit-button nit-button--secondary\" href=\"#practice\">Practise SQL<\/a>\n        <\/div>\n      <\/div>\n      <div class=\"nit-code-window\" role=\"region\" tabindex=\"0\" aria-label=\"SQL JOIN preview\">\n        <div class=\"nit-code-window__bar\" aria-hidden=\"true\">\n          <span class=\"nit-code-window__dot\"><\/span><span class=\"nit-code-window__dot\"><\/span><span class=\"nit-code-window__dot\"><\/span><span class=\"nit-code-window__file\">customers-orders.sql<\/span>\n        <\/div>\n        <div class=\"nit-code-window__body\">\n          <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span> c.customer_name,<\/span>\n          <span class=\"nit-code-line\">&#160;&#160;&#160;&#160;&#160;&#160; o.order_id<\/span>\n          <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span> customers c<\/span>\n          <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT JOIN<\/span> orders o<\/span>\n          <span class=\"nit-code-line\">&#160;&#160;<span class=\"nit-code-keyword\">ON<\/span> o.customer_id = c.customer_id;<\/span>\n        <\/div>\n      <\/div>\n    <\/div>\n  <\/div>\n  <div class=\"nit-article__body\">\n    <aside class=\"nit-quick-answer\" aria-label=\"Quick answer: SQL JOIN definition\">\n      <div class=\"nit-quick-answer__head\">\n        <span class=\"nit-quick-answer__mark\" aria-hidden=\"true\">\u2194<\/span>\n        <div><p class=\"nit-quick-answer__label\">QUICK ANSWER<\/p><p class=\"nit-quick-answer__kicker\">What is a SQL JOIN?<\/p><\/div>\n      <\/div>\n      <div class=\"nit-quick-answer__body\">\n        <p>A SQL JOIN combines related rows from two or more tables using a matching condition, usually involving IDs. An <strong>INNER JOIN<\/strong> keeps matching rows; a <strong>LEFT JOIN<\/strong> preserves all rows from the left table even when no match exists. In reporting, the join choice\u2014and what one output row represents\u2014determines whether counts and totals are trustworthy.<\/p>\n      <\/div>\n    <\/aside>\n    <div class=\"nit-facts-grid\" aria-label=\"Example dataset summary\">\n      <div class=\"nit-fact-card\"><span>Related tables<\/span><strong>4<\/strong><\/div>\n      <div class=\"nit-fact-card\"><span>Completed orders<\/span><strong>5<\/strong><\/div>\n      <div class=\"nit-fact-card\"><span>Completed-sales value<\/span><strong>KSh 7,900<\/strong><\/div>\n    <\/div>\n    <p>Imagine you are reviewing five days of transactions (1\u20135 October 2026) for a fictional Kenyan retailer. The manager asks three questions: <strong>Who purchased?<\/strong> <strong>Which products sold?<\/strong> and <strong>What is the sales value of the completed orders?<\/strong> Those answers are stored across different tables. By the end of this lesson, you will join them, check the row counts, and explain a KSh 7,900 completed-sales result.<\/p>\n    <p>This is a five-day sales snapshot, not a calendar-week report: 1\u20135 October crosses an ISO-week boundary. Here, \u201csales value\u201d means the sum of recorded order-line prices for completed orders; it is not a statement about cash collected, tax, refunds or profit. All sales-reporting queries below explicitly restrict the reporting window to <strong>1 October inclusive through 6 October exclusive<\/strong>, so they keep the same meaning if the sample database later grows.<\/p>\n    <p><strong>Before you start:<\/strong> Know the basics of <em>SELECT<\/em>, <em>FROM<\/em> and <em>WHERE<\/em>. If relational tables are new to you, begin with NIT\u2019s <a href=\"https:\/\/niteducation.com\/resources\/what-is-a-database\/\">database fundamentals guide<\/a> or <a href=\"https:\/\/niteducation.com\/resources\/what-is-sql-for-data-analysis\/\">SQL for Data Analysis introduction<\/a>.<\/p>\n    <nav class=\"nit-toc\" aria-label=\"Article contents\">\n      <h2>In this lesson<\/h2>\n      <ol>\n        <li><a href=\"#joins-basics\">What a SQL JOIN means<\/a><\/li>\n        <li><a href=\"#sales-data\">The four-table sales database<\/a><\/li>\n        <li><a href=\"#join-types\">INNER, LEFT, RIGHT and FULL JOIN<\/a><\/li>\n        <li><a href=\"#build-sales-report\">Build and verify the sales report<\/a><\/li>\n        <li><a href=\"#report-accuracy\">Fix wrong totals and missing records<\/a><\/li>\n        <li><a href=\"#practice\">Exercises with worked answers<\/a><\/li>\n        <li><a href=\"#faq\">Frequently asked questions<\/a><\/li>\n      <\/ol>\n    <\/nav>\n    <section id=\"joins-basics\" aria-labelledby=\"joins-basics-title\">\n      <h2 id=\"joins-basics-title\">What Is a SQL JOIN and Why Does It Matter?<\/h2>\n      <p>A JOIN pairs rows from related tables according to an ON condition. A matching customer_id connects a customer record with each of their orders. A customer may place many orders; each order may contain several product lines. Instead of repeating every customer detail on every line, a relational database stores each kind of record separately and links it by an ID. The <strong>ON<\/strong> condition must express that genuine relationship: matching columns merely because they are both called <strong>id<\/strong> can silently remove rows or combine unrelated records. <sup><a href=\"https:\/\/www.postgresql.org\/docs\/current\/tutorial-join.html\" rel=\"noopener noreferrer\" target=\"_blank\">[1]<\/a><\/sup><\/p>\n      <h3>Primary key<\/h3>\n      <p>A value that uniquely identifies a record: <strong>customers.customer_id<\/strong> identifies one customer.<\/p>\n      <h3>Foreign key<\/h3>\n      <p>A reference to another table: <strong>orders.customer_id<\/strong> points to a customer.<\/p>\n      <h3>Row grain<\/h3>\n      <p>The meaning of a single row. One row might represent an order\u2014or a single product line within an order.<\/p>\n      <p>For example, if Amina places two orders, she appears once in <strong>customers<\/strong> but twice in <strong>orders<\/strong>. A JOIN correctly creates two joined rows. That is not automatically a duplicate-data error: the output has a different grain.<\/p>\n      <p><strong>Beginner rule<\/strong><\/p>\n      <p>First state what one output row should represent. Then choose the JOIN and aggregation that produce that grain. This single habit prevents many reporting mistakes. If an order has three line items, an order-to-item join produces three rows <strong>by design<\/strong>, even though there is only one order.<\/p>\n    <\/section>\n    <section id=\"sales-data\" aria-labelledby=\"sales-data-title\">\n      <h2 id=\"sales-data-title\">Meet the Sales Database: Four Connected Tables<\/h2>\n      <p>This fictional training dataset uses Kenyan towns and prices in Kenyan shillings. It is designed for learning, not taken from real customers. The <strong>customers<\/strong> and <strong>products<\/strong> tables each contain five rows; there are six <strong>orders<\/strong> and nine <strong>order_items<\/strong>. Five orders are completed and one is cancelled.<\/p>\n      <p><strong>Relationship map<\/strong><\/p>\n      <p><strong>customers<\/strong> (1) \u2192 (many) <strong>orders<\/strong> (1) \u2192 (many) <strong>order_items<\/strong> (many) \u2190 (1) <strong>products<\/strong>. Match customer IDs, order IDs and product IDs\u2014not names.<\/p>\n      <h3>Table 1: customers \u2014 one row per customer<\/h3>\n      <div aria-label=\"SQL lesson reference table 1\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">customer_id<\/th>\n      <th scope=\"col\">customer_name<\/th>\n      <th scope=\"col\">city<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>1<\/td>\n      <td>Amina<\/td>\n      <td>Migori<\/td>\n      <\/tr>\n      <tr>\n      <td>2<\/td>\n      <td>Brian<\/td>\n      <td>Kisumu<\/td>\n      <\/tr>\n      <tr>\n      <td>3<\/td>\n      <td>Chao<\/td>\n      <td>Kisii<\/td>\n      <\/tr>\n      <tr>\n      <td>4<\/td>\n      <td>David<\/td>\n      <td>Nairobi<\/td>\n      <\/tr>\n      <tr>\n      <td>5<\/td>\n      <td>Esther<\/td>\n      <td>Homa Bay<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <h3>Table 2: orders \u2014 one row per order<\/h3>\n      <div aria-label=\"SQL lesson reference table 2\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">order_id<\/th>\n      <th scope=\"col\">order_date<\/th>\n      <th scope=\"col\">customer_id<\/th>\n      <th scope=\"col\">status<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>1001<\/td>\n      <td>2026-10-01<\/td>\n      <td>1<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>1002<\/td>\n      <td>2026-10-01<\/td>\n      <td>2<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>1003<\/td>\n      <td>2026-10-02<\/td>\n      <td>1<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>1004<\/td>\n      <td>2026-10-03<\/td>\n      <td>3<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>1005<\/td>\n      <td>2026-10-04<\/td>\n      <td>4<\/td>\n      <td>cancelled<\/td>\n      <\/tr>\n      <tr>\n      <td>1006<\/td>\n      <td>2026-10-05<\/td>\n      <td>2<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>David placed a cancelled order; Esther placed no order at all. Those two situations are different, and the JOIN examples will preserve that distinction.<\/p>\n      <h3>Table 3: products \u2014 one row per product<\/h3>\n      <div aria-label=\"SQL lesson reference table 3\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">product_id<\/th>\n      <th scope=\"col\">product_name<\/th>\n      <th scope=\"col\">list_price (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>101<\/td>\n      <td>Notebook<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>102<\/td>\n      <td>Headset<\/td>\n      <td>1,500<\/td>\n      <\/tr>\n      <tr>\n      <td>103<\/td>\n      <td>Mouse<\/td>\n      <td>900<\/td>\n      <\/tr>\n      <tr>\n      <td>104<\/td>\n      <td>Flash Drive<\/td>\n      <td>600<\/td>\n      <\/tr>\n      <tr>\n      <td>105<\/td>\n      <td>Webcam<\/td>\n      <td>2,500<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <h3>Table 4: order_items \u2014 one row per order line<\/h3>\n      <div aria-label=\"SQL lesson reference table 4\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">order_id<\/th>\n      <th scope=\"col\">product_id<\/th>\n      <th scope=\"col\">quantity<\/th>\n      <th scope=\"col\">unit_price (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>1001<\/td>\n      <td>101<\/td>\n      <td>2<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>1001<\/td>\n      <td>103<\/td>\n      <td>1<\/td>\n      <td>800<\/td>\n      <\/tr>\n      <tr>\n      <td>1002<\/td>\n      <td>102<\/td>\n      <td>1<\/td>\n      <td>1500<\/td>\n      <\/tr>\n      <tr>\n      <td>1002<\/td>\n      <td>104<\/td>\n      <td>2<\/td>\n      <td>600<\/td>\n      <\/tr>\n      <tr>\n      <td>1003<\/td>\n      <td>104<\/td>\n      <td>3<\/td>\n      <td>600<\/td>\n      <\/tr>\n      <tr>\n      <td>1004<\/td>\n      <td>101<\/td>\n      <td>1<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>1005<\/td>\n      <td>102<\/td>\n      <td>1<\/td>\n      <td>1500<\/td>\n      <\/tr>\n      <tr>\n      <td>1006<\/td>\n      <td>103<\/td>\n      <td>2<\/td>\n      <td>800<\/td>\n      <\/tr>\n      <tr>\n      <td>1006<\/td>\n      <td>101<\/td>\n      <td>1<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>The catalogue lists the Mouse at KSh 900, but the recorded sales price is KSh 800. Use <strong>order_items.unit_price<\/strong> for sales calculations\u2014historic transactions must not be recomputed from a current catalogue price. <strong>Line value = quantity \u00d7 unit_price.<\/strong><\/p>\n      <p><strong>Important reporting rule<\/strong><\/p>\n      <p>Cancelled order 1005 contains KSh 1,500 in order lines but is <strong>excluded<\/strong> from completed-order sales value. A correct report must filter by order status before summing.<\/p>\n      <h3>Set up the sample tables<\/h3>\n      <p>Run the PostgreSQL setup below in a dedicated practice connection (for example, a local PostgreSQL client or a pgAdmin Query Tool session). It creates four <strong>temporary<\/strong> tables and loads exactly the rows shown here. Run the setup once, then keep the same connection for the examples: temporary tables vanish at session end, and rerunning the CREATE statements in the same session without dropping the tables raises an \u201calready exists\u201d error. Work in a dedicated training database\/connection\u2014not against production records. This is intentionally a small learning schema, not a full retail accounting system.<\/p>\n      <h3>Open the full CREATE TABLE and INSERT setup SQL<\/h3>\n      <div aria-label=\"SQL example 1: sales-report-01.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-01.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">&#8212;\u00a0Article\u00a0273:\u00a0SQL\u00a0JOINs\u00a0Explained\u00a0\u2014\u00a0fictional\u00a0teaching\u00a0dataset<\/span>\n      <span class=\"nit-code-line\">&#8212;\u00a0PostgreSQL-compatible.\u00a0Run\u00a0in\u00a0one\u00a0connection\/session.<\/span>\n      <span class=\"nit-code-line\">&#8212;\u00a0TEMP\u00a0tables\u00a0protect\u00a0existing\u00a0permanent\u00a0business\u00a0tables.<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">CREATE<\/span>\u00a0<span class=\"nit-code-keyword\">TEMP<\/span>\u00a0<span class=\"nit-code-keyword\">TABLE<\/span>\u00a0customers\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0customer_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">PRIMARY<\/span>\u00a0<span class=\"nit-code-keyword\">KEY<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0customer_name\u00a0<span class=\"nit-code-keyword\">VARCHAR<\/span>(80)\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0city\u00a0<span class=\"nit-code-keyword\">VARCHAR<\/span>(60)\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span><\/span>\n      <span class=\"nit-code-line\">);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">CREATE<\/span>\u00a0<span class=\"nit-code-keyword\">TEMP<\/span>\u00a0<span class=\"nit-code-keyword\">TABLE<\/span>\u00a0products\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0product_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">PRIMARY<\/span>\u00a0<span class=\"nit-code-keyword\">KEY<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0product_name\u00a0<span class=\"nit-code-keyword\">VARCHAR<\/span>(80)\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0list_price\u00a0<span class=\"nit-code-keyword\">NUMERIC<\/span>(10,2)\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span><\/span>\n      <span class=\"nit-code-line\">);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">CREATE<\/span>\u00a0<span class=\"nit-code-keyword\">TEMP<\/span>\u00a0<span class=\"nit-code-keyword\">TABLE<\/span>\u00a0orders\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0order_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">PRIMARY<\/span>\u00a0<span class=\"nit-code-keyword\">KEY<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0customer_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>\u00a0<span class=\"nit-code-keyword\">REFERENCES<\/span>\u00a0customers(customer_id),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0order_date\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0status\u00a0<span class=\"nit-code-keyword\">VARCHAR<\/span>(20)\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>\u00a0<span class=\"nit-code-keyword\">CHECK<\/span>\u00a0(status\u00a0<span class=\"nit-code-keyword\">IN<\/span>\u00a0(&#8216;completed&#8217;,\u00a0&#8216;cancelled&#8217;))<\/span>\n      <span class=\"nit-code-line\">);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">CREATE<\/span>\u00a0<span class=\"nit-code-keyword\">TEMP<\/span>\u00a0<span class=\"nit-code-keyword\">TABLE<\/span>\u00a0order_items\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0item_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">PRIMARY<\/span>\u00a0<span class=\"nit-code-keyword\">KEY<\/span>,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0order_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>\u00a0<span class=\"nit-code-keyword\">REFERENCES<\/span>\u00a0orders(order_id),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0product_id\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>\u00a0<span class=\"nit-code-keyword\">REFERENCES<\/span>\u00a0products(product_id),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0quantity\u00a0<span class=\"nit-code-keyword\">INTEGER<\/span>\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>\u00a0<span class=\"nit-code-keyword\">CHECK<\/span>\u00a0(quantity\u00a0&gt;\u00a00),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0unit_price\u00a0<span class=\"nit-code-keyword\">NUMERIC<\/span>(10,2)\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span>\u00a0<span class=\"nit-code-keyword\">CHECK<\/span>\u00a0(unit_price\u00a0&gt;=\u00a00)<\/span>\n      <span class=\"nit-code-line\">);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">INSERT<\/span>\u00a0<span class=\"nit-code-keyword\">INTO<\/span>\u00a0customers\u00a0(customer_id,\u00a0customer_name,\u00a0city)\u00a0<span class=\"nit-code-keyword\">VALUES<\/span><\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1,\u00a0&#8216;Amina&#8217;,\u00a0&#8216;Migori&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(2,\u00a0&#8216;Brian&#8217;,\u00a0&#8216;Kisumu&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(3,\u00a0&#8216;Chao&#8217;,\u00a0&#8216;Kisii&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(4,\u00a0&#8216;David&#8217;,\u00a0&#8216;Nairobi&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(5,\u00a0&#8216;Esther&#8217;,\u00a0&#8216;Homa\u00a0Bay&#8217;);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">INSERT<\/span>\u00a0<span class=\"nit-code-keyword\">INTO<\/span>\u00a0products\u00a0(product_id,\u00a0product_name,\u00a0list_price)\u00a0<span class=\"nit-code-keyword\">VALUES<\/span><\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(101,\u00a0&#8216;Notebook&#8217;,\u00a0250),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(102,\u00a0&#8216;Headset&#8217;,\u00a01500),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(103,\u00a0&#8216;Mouse&#8217;,\u00a0900),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(104,\u00a0&#8216;Flash\u00a0Drive&#8217;,\u00a0600),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(105,\u00a0&#8216;Webcam&#8217;,\u00a02500);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">INSERT<\/span>\u00a0<span class=\"nit-code-keyword\">INTO<\/span>\u00a0orders\u00a0(order_id,\u00a0customer_id,\u00a0order_date,\u00a0status)\u00a0<span class=\"nit-code-keyword\">VALUES<\/span><\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1001,\u00a01,\u00a0&#8216;2026-10-01&#8217;,\u00a0&#8216;completed&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1002,\u00a02,\u00a0&#8216;2026-10-01&#8217;,\u00a0&#8216;completed&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1003,\u00a01,\u00a0&#8216;2026-10-02&#8217;,\u00a0&#8216;completed&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1004,\u00a03,\u00a0&#8216;2026-10-03&#8217;,\u00a0&#8216;completed&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1005,\u00a04,\u00a0&#8216;2026-10-04&#8217;,\u00a0&#8216;cancelled&#8217;),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1006,\u00a02,\u00a0&#8216;2026-10-05&#8217;,\u00a0&#8216;completed&#8217;);<\/span>\n      <span class=\"nit-code-line\">\u00a0<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">INSERT<\/span>\u00a0<span class=\"nit-code-keyword\">INTO<\/span>\u00a0order_items\u00a0(item_id,\u00a0order_id,\u00a0product_id,\u00a0quantity,\u00a0unit_price)\u00a0<span class=\"nit-code-keyword\">VALUES<\/span><\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(1,\u00a01001,\u00a0101,\u00a02,\u00a0250),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(2,\u00a01001,\u00a0103,\u00a01,\u00a0800),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(3,\u00a01002,\u00a0102,\u00a01,\u00a01500),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(4,\u00a01002,\u00a0104,\u00a02,\u00a0600),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(5,\u00a01003,\u00a0104,\u00a03,\u00a0600),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(6,\u00a01004,\u00a0101,\u00a01,\u00a0250),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(7,\u00a01005,\u00a0102,\u00a01,\u00a01500),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(8,\u00a01006,\u00a0103,\u00a02,\u00a0800),<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0(9,\u00a01006,\u00a0101,\u00a01,\u00a0250);<\/span>\n      <\/div>\n      <\/div>\n      <h3>Dialect note<\/h3>\n      <p>The runnable examples use <strong>PostgreSQL SQL<\/strong>, including <strong>DATE &#8216;YYYY-MM-DD&#8217;<\/strong> literals. Some database engines use different syntax or do not support every JOIN type. The sample calculations were cross-checked in SQLite, with its date-literal syntax adapted for testing; direct PostgreSQL execution is a separate release check. Real business rules may require additional treatment of returns, refunds, taxes, shipping and payment allocation. PostgreSQL\u2019s <a href=\"https:\/\/www.postgresql.org\/docs\/current\/queries-table-expressions.html\" target=\"_blank\" rel=\"noopener\">official table-expression reference<\/a> documents the JOIN and <strong>ON<\/strong>\/<strong>WHERE<\/strong> semantics used here.<\/p>\n    <\/section>\n    <section id=\"join-types\" aria-labelledby=\"join-types-title\">\n      <h2 id=\"join-types-title\">SQL JOIN Types Explained With Examples<\/h2>\n      <p>The same relationship produces different results depending on whether unmatched rows are retained. Follow the query and its expected output, then predict which customers will disappear or remain. <strong>JOIN<\/strong> without a modifier means <strong>INNER JOIN<\/strong>; <strong>LEFT OUTER JOIN<\/strong> and <strong>LEFT JOIN<\/strong> are interchangeable spellings in PostgreSQL.<\/p>\n      <h3>INNER JOIN: keep matching customers and orders<\/h3>\n      <p>An INNER JOIN returns an order only when a related customer exists. With our foreign-key-protected sample, all six orders match customers, so the result contains six rows\u2014including the cancelled order. A JOIN does not automatically filter business status.<\/p>\n      <div aria-label=\"SQL example 2: sales-report-02.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-02.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,\u00a0o.order_id,\u00a0o.status<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">INNER<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0o.order_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 5\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Customer<\/th>\n      <th scope=\"col\">Order<\/th>\n      <th scope=\"col\">Status<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Amina<\/td>\n      <td>1001<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>Brian<\/td>\n      <td>1002<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>Amina<\/td>\n      <td>1003<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>Chao<\/td>\n      <td>1004<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <tr>\n      <td>David<\/td>\n      <td>1005<\/td>\n      <td>cancelled<\/td>\n      <\/tr>\n      <tr>\n      <td>Brian<\/td>\n      <td>1006<\/td>\n      <td>completed<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p><strong>Why Amina appears twice<\/strong><\/p>\n      <p>Amina placed orders 1001 and 1003. The JOIN returns one row per order; this is correct one-to-many behaviour.<\/p>\n      <h3>LEFT JOIN: keep customers even without completed orders<\/h3>\n      <p>For a customer report, begin with <strong>customers<\/strong> and LEFT JOIN only completed orders <strong>within the five-day window<\/strong>. Keep the right-table status <strong>and date<\/strong> conditions in <strong>ON<\/strong> so that customers with zero qualifying orders are preserved.<\/p>\n      <div aria-label=\"SQL example 3: sales-report-03.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-03.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(o.order_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_orders<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id,\u00a0c.customer_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 6\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Customer<\/th>\n      <th scope=\"col\">Completed orders<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Amina<\/td>\n      <td>2<\/td>\n      <\/tr>\n      <tr>\n      <td>Brian<\/td>\n      <td>2<\/td>\n      <\/tr>\n      <tr>\n      <td>Chao<\/td>\n      <td>1<\/td>\n      <\/tr>\n      <tr>\n      <td>David<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <tr>\n      <td>Esther<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>David has zero completed orders because his only order was cancelled. Esther has zero because she never ordered. Both remain visible.<\/p>\n      <h3>RIGHT JOIN: preserve the right-hand table<\/h3>\n      <p>A RIGHT JOIN keeps every row from the table written to the right of the JOIN. This query keeps all five customers, including Esther. Most beginner reports are easier to read if you swap table order and use LEFT JOIN instead.<\/p>\n      <div aria-label=\"SQL example 4: sales-report-04.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-04.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">RIGHT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0c.customer_id\u00a0=\u00a0o.customer_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id,\u00a0o.order_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 7\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Customer<\/th>\n      <th scope=\"col\">Order<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Amina<\/td>\n      <td>1001<\/td>\n      <\/tr>\n      <tr>\n      <td>Amina<\/td>\n      <td>1003<\/td>\n      <\/tr>\n      <tr>\n      <td>Brian<\/td>\n      <td>1002<\/td>\n      <\/tr>\n      <tr>\n      <td>Brian<\/td>\n      <td>1006<\/td>\n      <\/tr>\n      <tr>\n      <td>Chao<\/td>\n      <td>1004<\/td>\n      <\/tr>\n      <tr>\n      <td>David<\/td>\n      <td>1005<\/td>\n      <\/tr>\n      <tr>\n      <td>Esther<\/td>\n      <td>NULL<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>There are <strong>seven rows<\/strong> because Esther survives without an order, and Amina and Brian each have two orders. The cancelled order 1005 still appears: this query has no order-status filter. Swapping table order and using <strong>LEFT JOIN<\/strong> produces equivalent preservation behaviour.<\/p>\n      <h3>FULL OUTER JOIN: keep unmatched rows from both sides<\/h3>\n      <p>FULL OUTER JOIN is useful for reconciling two independent lists. Suppose a manager has targets for Migori, Kisumu and Nakuru, while completed sales exist in Migori, Kisumu and Kisii. That creates a match, a sales-only city and a target-only city.<\/p>\n      <div aria-label=\"SQL example 5: sales-report-05.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-05.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WITH<\/span>\u00a0targets(city,\u00a0target_ksh)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">VALUES<\/span>\u00a0(&#8216;Migori&#8217;,\u00a03000),\u00a0(&#8216;Kisumu&#8217;,\u00a04500),\u00a0(&#8216;Nakuru&#8217;,\u00a02000)<\/span>\n      <span class=\"nit-code-line\">),\u00a0actuals\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.city,\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity\u00a0*\u00a0oi.unit_price)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0actual_ksh<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.city<\/span>\n      <span class=\"nit-code-line\">)<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0<span class=\"nit-code-keyword\">COALESCE<\/span>(t.city,\u00a0a.city)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0city,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t.target_ksh,\u00a0a.actual_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0targets\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0t<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FULL<\/span>\u00a0<span class=\"nit-code-keyword\">OUTER<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0actuals\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0a\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0a.city\u00a0=\u00a0t.city<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0city;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 8\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">City<\/th>\n      <th scope=\"col\">Target (KSh)<\/th>\n      <th scope=\"col\">Actual (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Kisii<\/td>\n      <td>\u2014<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>Kisumu<\/td>\n      <td>4,500<\/td>\n      <td>4,550<\/td>\n      <\/tr>\n      <tr>\n      <td>Migori<\/td>\n      <td>3,000<\/td>\n      <td>3,100<\/td>\n      <\/tr>\n      <tr>\n      <td>Nakuru<\/td>\n      <td>2,000<\/td>\n      <td>\u2014<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>Kisii appears with an actual amount but no target. Nakuru appears with a target but no actual sales entry. These are genuinely unmatched rows on opposite sides; the dash indicates SQL NULL, not zero. Migori and Kisumu have both values.<\/p>\n      <h3>SQL JOIN types compared<\/h3>\n      <div aria-label=\"SQL lesson reference table 9\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">JOIN<\/th>\n      <th scope=\"col\">What it preserves<\/th>\n      <th scope=\"col\">Beginner use<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>INNER<\/td>\n      <td>Only matching rows<\/td>\n      <td>Orders with a matching customer<\/td>\n      <\/tr>\n      <tr>\n      <td>LEFT<\/td>\n      <td>Every row on the left<\/td>\n      <td>All customers, including non-buyers<\/td>\n      <\/tr>\n      <tr>\n      <td>RIGHT<\/td>\n      <td>Every row on the right<\/td>\n      <td>Equivalent preservation with tables reversed<\/td>\n      <\/tr>\n      <tr>\n      <td>FULL OUTER<\/td>\n      <td>All matched and unmatched rows on either side<\/td>\n      <td>Compare two lists with missing members<\/td>\n      <\/tr>\n      <tr>\n      <td>CROSS<\/td>\n      <td>Every pair of rows<\/td>\n      <td>Generate combinations intentionally<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <h3>Two other JOIN patterns you will encounter<\/h3>\n      <p><strong>CROSS JOIN<\/strong> creates every combination of rows rather than matching keys. Five customers crossed with six orders give 5 \u00d7 6 = <strong>30 combinations<\/strong>. Use it only when every pairing is intentional; a JOIN with an overly broad <strong>ON<\/strong> condition can also multiply rows unexpectedly:<\/p>\n      <div aria-label=\"SQL example 6: sales-report-06.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-06.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(*)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0combinations<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">CROSS<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders;<\/span>\n      <span class=\"nit-code-line\">&#8212;\u00a0Result:\u00a030<\/span>\n      <\/div>\n      <\/div>\n      <div class=\"nit-callout nit-callout--info\"><p class=\"nit-callout__title\">Self-join: compare two orders<\/p>\n      <p>A <strong>self-join<\/strong> uses the same table twice under different aliases. The next query finds pairs of orders placed by the same customer, without repeating the reversed pair:<\/p>\n      <\/div>\n      <div aria-label=\"SQL example 7: sales-report-07.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-07.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0a.customer_id,\u00a0a.order_id\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0first_order,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0b.order_id\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0second_order<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0a<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0b<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0b.customer_id\u00a0=\u00a0a.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0a.order_id\u00a0&lt;\u00a0b.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0a.customer_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 10\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">customer_id<\/th>\n      <th scope=\"col\">first_order<\/th>\n      <th scope=\"col\">second_order<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>1<\/td>\n      <td>1001<\/td>\n      <td>1003<\/td>\n      <\/tr>\n      <tr>\n      <td>2<\/td>\n      <td>1002<\/td>\n      <td>1006<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>The self-join shows relationships <em>within<\/em> orders; it does not calculate revenue and is not needed for the main four-table report. <a href=\"https:\/\/www.postgresql.org\/docs\/current\/tutorial-join.html\" target=\"_blank\" rel=\"noopener\">https:\/\/www.postgresql.org\/docs\/current\/tutorial-join.html<\/a> explains this pattern in PostgreSQL&#8217;s official tutorial.<\/p>\n    <\/section>\n    <section id=\"build-sales-report\" aria-labelledby=\"build-sales-report-title\">\n      <h2 id=\"build-sales-report-title\">Build a Sales Report From Four SQL Tables<\/h2>\n      <p>We will now create the report progressively. The reporting grain first becomes <strong>one order item<\/strong>; later, GROUP BY summarises those detail rows into products or customers.<\/p>\n      <h3>Decide which records belong in the report<\/h3>\n      <p>Our training file contains orders dated <strong>1\u20135 October 2026<\/strong>. That is the complete sample window, but an operational report must explicitly define its own start and end dates rather than silently relying on the contents of a table. For a period that includes 1 October and ends after 5 October, an inclusive-start\/exclusive-end filter is:<\/p>\n      <div aria-label=\"SQL example 8: sales-report-08.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-08.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <\/div>\n      <\/div>\n      <div class=\"nit-callout nit-callout--info\"><p class=\"nit-callout__title\">Keep the reporting period consistent<\/p>\n      <p>The completed-sales queries below already include these date bounds. For a different reporting period, update the start and exclusive end dates consistently across every relevant query. A half-open interval also works cleanly with timestamp columns when appropriate: it avoids guessing the final time-of-day value. For the sample dataset, the results remain <strong>five completed orders and KSh 7,900<\/strong>. Completed order status does not establish that a customer has paid; actual cash collection, taxes, discounts and refunds require their own records and business rules.<\/p>\n      <\/div>\n      <h3>Step 1: Join customers, orders, order items and products<\/h3>\n      <p>Every required relationship must appear in the query. Use aliases to shorten SQL while keeping column origins clear. The WHERE clause includes only completed orders within the five-day reporting period.<\/p>\n      <div aria-label=\"SQL example 9: sales-report-09.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-09.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0o.order_id,\u00a0c.customer_name,\u00a0p.product_name,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0oi.quantity,\u00a0oi.unit_price,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0oi.quantity\u00a0*\u00a0oi.unit_price\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0line_revenue<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0c.customer_id\u00a0=\u00a0o.customer_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0products\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0p\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0p.product_id\u00a0=\u00a0oi.product_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0o.order_id,\u00a0oi.item_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 11\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Order<\/th>\n      <th scope=\"col\">Customer<\/th>\n      <th scope=\"col\">Product<\/th>\n      <th scope=\"col\">Line revenue (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>1001<\/td>\n      <td>Amina<\/td>\n      <td>Notebook<\/td>\n      <td>500<\/td>\n      <\/tr>\n      <tr>\n      <td>1001<\/td>\n      <td>Amina<\/td>\n      <td>Mouse<\/td>\n      <td>800<\/td>\n      <\/tr>\n      <tr>\n      <td>1002<\/td>\n      <td>Brian<\/td>\n      <td>Headset<\/td>\n      <td>1,500<\/td>\n      <\/tr>\n      <tr>\n      <td>1002<\/td>\n      <td>Brian<\/td>\n      <td>Flash Drive<\/td>\n      <td>1,200<\/td>\n      <\/tr>\n      <tr>\n      <td>1003<\/td>\n      <td>Amina<\/td>\n      <td>Flash Drive<\/td>\n      <td>1,800<\/td>\n      <\/tr>\n      <tr>\n      <td>1004<\/td>\n      <td>Chao<\/td>\n      <td>Notebook<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>1006<\/td>\n      <td>Brian<\/td>\n      <td>Mouse<\/td>\n      <td>1,600<\/td>\n      <\/tr>\n      <tr>\n      <td>1006<\/td>\n      <td>Brian<\/td>\n      <td>Notebook<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>Notice that order 1001 appears twice because it contains two products. This is expected: the query produces one row per <strong>order item<\/strong>. The eight lines are not eight separate customer orders.<\/p>\n      <h3>Step 2: Calculate revenue by product<\/h3>\n      <p>Multiply quantity by the selling price stored on the order line, exclude cancelled orders, then group by product ID and name.<\/p>\n      <div aria-label=\"SQL example 10: sales-report-10.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-10.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0p.product_name,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0units_sold,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity\u00a0*\u00a0oi.unit_price)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0revenue_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0products\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0p<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.product_id\u00a0=\u00a0p.product_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.order_id\u00a0=\u00a0oi.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0p.product_id,\u00a0p.product_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0revenue_ksh\u00a0<span class=\"nit-code-keyword\">DESC<\/span>,\u00a0p.product_name;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 12\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Product<\/th>\n      <th scope=\"col\">Units sold<\/th>\n      <th scope=\"col\">Revenue (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Flash Drive<\/td>\n      <td>5<\/td>\n      <td>3,000<\/td>\n      <\/tr>\n      <tr>\n      <td>Mouse<\/td>\n      <td>3<\/td>\n      <td>2,400<\/td>\n      <\/tr>\n      <tr>\n      <td>Headset<\/td>\n      <td>1<\/td>\n      <td>1,500<\/td>\n      <\/tr>\n      <tr>\n      <td>Notebook<\/td>\n      <td>4<\/td>\n      <td>1,000<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>Adding the four product totals gives <strong>KSh 7,900<\/strong>. The Webcam is missing here because it has no completed sales; an INNER JOIN excludes unmatched products. Use a LEFT JOIN when a report must also display unsold stock items.<\/p>\n      <h3>Bonus: include unsold products with a LEFT JOIN<\/h3>\n      <p>For a complete product catalogue report, start from products, LEFT JOIN order items and orders, and count revenue only when the order is completed <strong>during the reporting window<\/strong>. This keeps Webcam visible with KSh 0 in completed sales. Notice that a conditional <strong>CASE<\/strong> counts qualifying order lines without discarding unmatched products.<\/p>\n      <div aria-label=\"SQL example 11: sales-report-11.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-11.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0p.product_name,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COALESCE<\/span>(<span class=\"nit-code-keyword\">SUM<\/span>(<span class=\"nit-code-keyword\">CASE<\/span>\u00a0<span class=\"nit-code-keyword\">WHEN<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">THEN<\/span>\u00a0oi.quantity\u00a0*\u00a0oi.unit_price<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ELSE<\/span>\u00a00\u00a0<span class=\"nit-code-keyword\">END<\/span>),\u00a00)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0revenue_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0products\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0p<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.product_id\u00a0=\u00a0p.product_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.order_id\u00a0=\u00a0oi.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0p.product_id,\u00a0p.product_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0p.product_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 13\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Product<\/th>\n      <th scope=\"col\">Completed revenue (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Notebook<\/td>\n      <td>1,000<\/td>\n      <\/tr>\n      <tr>\n      <td>Headset<\/td>\n      <td>1,500<\/td>\n      <\/tr>\n      <tr>\n      <td>Mouse<\/td>\n      <td>2,400<\/td>\n      <\/tr>\n      <tr>\n      <td>Flash Drive<\/td>\n      <td>3,000<\/td>\n      <\/tr>\n      <tr>\n      <td>Webcam<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <h3>Step 3: Produce a customer report, including zero sales<\/h3>\n      <p>Start from customers and LEFT JOIN only completed orders in the reporting period, followed by their line items. <strong>COUNT(DISTINCT o.order_id)<\/strong> counts orders rather than repeated item lines. <strong>COALESCE<\/strong> converts a missing SUM to zero for presentation. This measures <strong>completed-order line value<\/strong>, not payment receipts or profit.<\/p>\n      <div aria-label=\"SQL example 12: sales-report-12.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-12.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,\u00a0c.city,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(<span class=\"nit-code-keyword\">DISTINCT<\/span>\u00a0o.order_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_orders,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COALESCE<\/span>(<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity\u00a0*\u00a0oi.unit_price),\u00a00)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0revenue_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id,\u00a0c.customer_name,\u00a0c.city<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0revenue_ksh\u00a0<span class=\"nit-code-keyword\">DESC<\/span>,\u00a0c.customer_id;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 14\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Customer<\/th>\n      <th scope=\"col\">City<\/th>\n      <th scope=\"col\">Orders<\/th>\n      <th scope=\"col\">Revenue (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Brian<\/td>\n      <td>Kisumu<\/td>\n      <td>2<\/td>\n      <td>4,550<\/td>\n      <\/tr>\n      <tr>\n      <td>Amina<\/td>\n      <td>Migori<\/td>\n      <td>2<\/td>\n      <td>3,100<\/td>\n      <\/tr>\n      <tr>\n      <td>Chao<\/td>\n      <td>Kisii<\/td>\n      <td>1<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>David<\/td>\n      <td>Nairobi<\/td>\n      <td>0<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <tr>\n      <td>Esther<\/td>\n      <td>Homa Bay<\/td>\n      <td>0<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>The report covers every customer, yet only completed order lines contribute to revenue. Brian generated KSh 4,550 and Amina KSh 3,100. A high revenue figure in this tiny demonstration is not evidence of customer profitability; we have no cost or margin data.<\/p>\n      <h3>Check the totals by order before trusting the grand total<\/h3>\n      <p>One order can have multiple lines, so first add the lines within each completed order:<\/p>\n      <div aria-label=\"SQL lesson reference table 15\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Completed order<\/th>\n      <th scope=\"col\">Calculation<\/th>\n      <th scope=\"col\">Order sales value (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>1001<\/td>\n      <td>500 + 800<\/td>\n      <td>1,300<\/td>\n      <\/tr>\n      <tr>\n      <td>1002<\/td>\n      <td>1,500 + 1,200<\/td>\n      <td>2,700<\/td>\n      <\/tr>\n      <tr>\n      <td>1003<\/td>\n      <td>1,800<\/td>\n      <td>1,800<\/td>\n      <\/tr>\n      <tr>\n      <td>1004<\/td>\n      <td>250<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>1006<\/td>\n      <td>1,600 + 250<\/td>\n      <td>1,850<\/td>\n      <\/tr>\n      <tr>\n      <td><strong>Total<\/strong><\/td>\n      <td><strong>1,300 + 2,700 + 1,800 + 250 + 1,850<\/strong><\/td>\n      <td><strong>7,900<\/strong><\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p>This hand-check provides a direct way to spot missing or repeated line amounts. It also explains why the cancelled order 1005&#8217;s KSh 1,500 must not appear in the total.<\/p>\n      <h3>Step 4: Reconcile the final report against the details<\/h3>\n      <p><strong>Check the order headers first.<\/strong> A completed order with zero order lines disappears when an <strong>INNER JOIN<\/strong> is applied to <strong>order_items<\/strong>; counting the joined rows alone would miss that header. The next query counts qualifying <strong>orders<\/strong> directly and flags any completed order without at least one line:<\/p>\n      <div aria-label=\"SQL example 13: sales-report-13.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-13.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(*)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_order_headers,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(<span class=\"nit-code-keyword\">CASE<\/span>\u00a0<span class=\"nit-code-keyword\">WHEN<\/span>\u00a0<span class=\"nit-code-keyword\">NOT<\/span>\u00a0<span class=\"nit-code-keyword\">EXISTS<\/span>\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SELECT<\/span>\u00a01<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">FROM<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">WHERE<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)\u00a0<span class=\"nit-code-keyword\">THEN<\/span>\u00a01\u00a0<span class=\"nit-code-keyword\">ELSE<\/span>\u00a00\u00a0<span class=\"nit-code-keyword\">END<\/span>)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0empty_completed_orders<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 16\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Completed order headers<\/th>\n      <th scope=\"col\">Empty completed orders<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>5<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p><strong>Now check the completed line items.<\/strong> This second query intentionally joins orders to order lines. It should return <strong>five distinct joined orders, eight completed lines, and KSh 7,900<\/strong>. Compare its order count with the independent header count above; the two counts agreeing is reassuring, but it does not prove the original transactions were recorded correctly.<\/p>\n      <div aria-label=\"SQL example 14: sales-report-14.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-14.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(<span class=\"nit-code-keyword\">DISTINCT<\/span>\u00a0o.order_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_orders,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(oi.item_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_lines,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity\u00a0*\u00a0oi.unit_price)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0total_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 17\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">Completed orders<\/th>\n      <th scope=\"col\">Completed lines<\/th>\n      <th scope=\"col\">Revenue (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>5<\/td>\n      <td>8<\/td>\n      <td>7,900<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p><strong>Dataset reconciliation result<\/strong><\/p>\n      <p>The order-header check finds <strong>5<\/strong> completed orders and <strong>0<\/strong> empty completed orders. The line-level check finds <strong>5<\/strong> represented orders and <strong>8<\/strong> lines. Product revenue totals (KSh 7,900), customer revenue totals (KSh 7,900), and line-level revenue (KSh 7,900) agree. These checks detect some common aggregation problems; they do not independently certify that every real-world sale, return, discount or payment was captured in the source system.<\/p>\n    <\/section>\n    <section id=\"report-accuracy\" aria-labelledby=\"report-accuracy-title\">\n      <h2 id=\"report-accuracy-title\">Why JOINs Produce Wrong Counts or Inflated Sales<\/h2>\n      <p>A SQL query can run successfully and still answer the wrong business question. Check the output grain before using <strong>COUNT<\/strong>, <strong>SUM<\/strong> or averages. These examples illustrate common errors that a simple SQL syntax checker cannot catch. <sup><a href=\"https:\/\/www.postgresql.org\/docs\/current\/queries-table-expressions.html\" rel=\"noopener noreferrer\" target=\"_blank\">[2]<\/a><\/sup><\/p>\n      <h3>Mistake 1: <strong>COUNT(*)<\/strong> treats order lines as customer orders<\/h3>\n      <p>The four-table report returns eight rows for only five completed orders. <strong><strong>COUNT(*)<\/strong> = 8<\/strong> counts joined lines. To count distinct completed orders after a line-level join, use <strong>COUNT(DISTINCT o.order_id) = 5<\/strong>.<\/p>\n      <p>Also be careful with LEFT JOIN: <strong>COUNT(*)<\/strong> counts the single preserved row for a customer with no order, while COUNT(o.order_id) returns zero because the joined order ID is NULL. COUNT(DISTINCT o.order_id) is helpful once each order has been repeated across multiple items.<\/p>\n      <h3>Mistake 2: Repeating an order total on every item line<\/h3>\n      <p>Suppose we first calculate one order total, then join it back to all the order\u2019s items. An order with two lines appears twice and its full order value is counted twice. This deliberately incorrect query produces <strong>KSh 13,750<\/strong> rather than KSh 7,900.<\/p>\n      <div aria-label=\"SQL example 15: sales-report-15.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-15.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WITH<\/span>\u00a0order_totals\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0(<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">SELECT<\/span>\u00a0order_id,\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(quantity\u00a0*\u00a0unit_price)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0order_total<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">FROM<\/span>\u00a0order_items<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0order_id<\/span>\n      <span class=\"nit-code-line\">)<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(ot.order_total)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0inflated_total_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_totals\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0ot\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0ot.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Why KSh 13,750 is wrong<\/h3>\n      <p>Order 1001 totals KSh 1,300 but appears on two item rows, contributing KSh 2,600. The same multiplication affects other multi-line orders. The sum is inflated by KSh 5,850.<\/p>\n      <p>For this completed-sales report, the correct calculation sums each order item\u2019s <strong>quantity \u00d7 unit_price<\/strong> exactly once. If you need order-level totals, aggregate first and do not then re-sum those totals after expanding them to line level. <strong>Do not try to fix this by writing <strong>SUM(DISTINCT order_total)<\/strong>:<\/strong> two different orders can have the same amount. If two genuine orders are each worth KSh 1,300, their combined value is KSh 2,600, but <strong>SUM(DISTINCT order_total)<\/strong> would count that KSh 1,300 only once. The right fix is to aggregate at the intended row grain.<\/p>\n      <div aria-label=\"SQL example 16: sales-report-16.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-16.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity\u00a0*\u00a0oi.unit_price)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0correct_total_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Correct total<\/h3>\n      <p>The corrected query returns <strong>KSh 7,900<\/strong>. It excludes the cancelled order and counts each completed order line once.<\/p>\n      <h3>Mistake 3: Putting an outer-join filter in WHERE<\/h3>\n      <p>When you put <strong>o.status = &#8216;completed&#8217;<\/strong> in the WHERE clause after a LEFT JOIN, SQL removes customers with NULL order fields. David and Esther disappear, so a report intended to show every customer no longer does so. PostgreSQL distinguishes the JOIN condition from filtering applied after the join. <a href=\"https:\/\/www.postgresql.org\/docs\/current\/queries-table-expressions.html\" target=\"_blank\" rel=\"noopener\">See the official table-expression rules<\/a>.<\/p>\n      <div aria-label=\"SQL example 17: sales-report-17.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-17.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(o.order_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_orders<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id,\u00a0c.customer_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Lost rows in the WHERE version<\/h3>\n      <p>This query returns only Amina, Brian and Chao. The corrected version below puts the status restriction in ON, preserving David and Esther with zero completed orders.<\/p>\n      <div aria-label=\"SQL example 18: sales-report-18.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-18.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(o.order_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_orders<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id,\u00a0c.customer_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Remember ON versus WHERE<\/h3>\n      <p>For this LEFT JOIN, putting a right-table filter in ON affects which orders match; putting it in WHERE removes unmatched customers afterward. For an INNER JOIN, moving this kind of filter between ON and WHERE is equivalent here; with an OUTER JOIN, the change can remove the rows you meant to preserve.<\/p>\n      <h3>Mistake 4: Joining unrelated ID columns<\/h3>\n      <p>Check the meaning of every <strong>ON<\/strong> comparison. <strong>order_items.item_id<\/strong> identifies an individual line, while <strong>order_items.product_id<\/strong> identifies the product sold. Joining <strong>products.product_id<\/strong> to <strong>order_items.item_id<\/strong> would return <strong>zero matches<\/strong> in this dataset, even though the SQL is syntactically valid. The correct relationship is <strong>products.product_id = order_items.product_id<\/strong>. Naming and foreign-key constraints help, but always verify that the matching columns refer to the same real-world entity.<\/p>\n      <h3>Quick diagnostic: which JOIN would you choose?<\/h3>\n      <ul>\n      <li><strong>Only orders with a valid customer:<\/strong> <strong>INNER JOIN<\/strong> customers to orders.<\/li>\n      <li><strong>Every customer, including non-buyers:<\/strong> <strong>customers LEFT JOIN orders<\/strong>.<\/li>\n      <li><strong>Every product, even with zero completed sales:<\/strong> start from products and <strong>LEFT JOIN<\/strong> the sales-side rows.<\/li>\n      <li><strong>A complete comparison of targets and actuals, even if one side is missing:<\/strong> <strong>FULL OUTER JOIN<\/strong> two independently prepared lists.<\/li>\n      <\/ul>\n      <p>The starting table is as important as the JOIN keyword. Always ask which records must survive when no match exists. <strong>USING (customer_id)<\/strong> can abbreviate an equality join when the key has the same name on both sides, but explicit <strong>ON<\/strong> is easier for mixed keys and extra filtering. Avoid relying on <strong>NATURAL JOIN<\/strong> for critical reports: it automatically joins on every shared column name, so an innocent schema change can alter the matching logic. See the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/queries-table-expressions.html\" target=\"_blank\" rel=\"noopener\">PostgreSQL JOIN reference<\/a>.<\/p>\n      <h3>A practical checklist before sharing a report<\/h3>\n      <ul>\n      <li>State the reporting period, order-status rules, and whether refunds or returns are included.<\/li>\n      <li>Identify whether each row represents a customer, order, order line or product.<\/li>\n      <li>Check every JOIN key and whether the relationship is one-to-one or one-to-many.<\/li>\n      <li>Compare <strong><strong>COUNT(*)<\/strong><\/strong> with <strong>COUNT(DISTINCT order_id)<\/strong> when counting orders.<\/li>\n      <li>Check how cancelled orders, NULLs and customers with zero orders are handled.<\/li>\n      <li>Reconcile line-level, customer-level and product-level revenue totals; separately count qualifying order headers and flag those without lines.<\/li>\n      <li>Make the report\u2019s assumptions explicit before a manager makes a decision.<\/li>\n      <\/ul>\n    <\/section>\n    <section id=\"practice\" aria-labelledby=\"practice-title\">\n      <h2 id=\"practice-title\">Practice SQL JOINs: Five Exercises and Answers<\/h2>\n      <p>Run the setup script first, then try each question without copying the solution immediately. Predict the result before executing your SQL.<\/p>\n      <ol>\n      <li>List every customer and the number of completed orders during 1\u20135 October, including zero.<\/li>\n      <li>Find customers who have <strong>never placed any order<\/strong>, including cancelled ones.<\/li>\n      <li>Find customers with <strong>no completed orders during 1\u20135 October<\/strong>, even if they have cancelled orders.<\/li>\n      <li>Find products with no completed-order sales <strong>during 1\u20135 October<\/strong>.<\/li>\n      <li>Calculate completed-order revenue <strong>during 1\u20135 October<\/strong> by city, including cities with zero revenue.<\/li>\n      <\/ol>\n      <h3>Answer 1: completed order counts<\/h3>\n      <p>Use LEFT JOIN, put the completed status in ON and count the right-table order ID. Expected counts: Amina 2; Brian 2; Chao 1; David 0; Esther 0.<\/p>\n      <div aria-label=\"SQL example 19: sales-report-19.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-19.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(o.order_id)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0completed_orders<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id,\u00a0c.customer_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_id;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Answer 2: customers who never ordered<\/h3>\n      <div aria-label=\"SQL example 20: sales-report-20.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-20.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.order_id\u00a0<span class=\"nit-code-keyword\">IS<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span><\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_name;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Expected result<\/h3>\n      <p>Esther only. David does not qualify because order 1005 exists, although cancelled.<\/p>\n      <h3>Answer 3: customers with no completed orders<\/h3>\n      <div aria-label=\"SQL example 21: sales-report-21.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-21.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.customer_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">WHERE<\/span>\u00a0o.order_id\u00a0<span class=\"nit-code-keyword\">IS<\/span>\u00a0<span class=\"nit-code-keyword\">NULL<\/span><\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.customer_name;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Expected result<\/h3>\n      <p>David and Esther. This differs from Exercise 2 because the status condition changes what counts as a match.<\/p>\n      <h3>Answer 4: products without completed sales<\/h3>\n      <div aria-label=\"SQL example 22: sales-report-22.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-22.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0p.product_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0products\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0p<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.product_id\u00a0=\u00a0p.product_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.order_id\u00a0=\u00a0oi.order_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0p.product_id,\u00a0p.product_name<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">HAVING<\/span>\u00a0<span class=\"nit-code-keyword\">COUNT<\/span>(o.order_id)\u00a0=\u00a00<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0p.product_name;<\/span>\n      <\/div>\n      <\/div>\n      <h3>Expected result<\/h3>\n      <p>Webcam. The <strong>HAVING<\/strong> clause counts only order-item matches connected to orders completed <strong>within the reporting dates<\/strong>. A product sold only before or after this period would also count as unsold <em>in this period<\/em>, not necessarily unsold forever.<\/p>\n      <h3>Answer 5: completed revenue by city<\/h3>\n      <div aria-label=\"SQL example 23: sales-report-23.sql\" class=\"nit-code-window\" role=\"region\" tabindex=\"0\">\n      <div aria-hidden=\"true\" class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">sales-report-23.sql<\/span>\n      <\/div>\n      <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">SELECT<\/span>\u00a0c.city,<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">COALESCE<\/span>(<span class=\"nit-code-keyword\">SUM<\/span>(oi.quantity\u00a0*\u00a0oi.unit_price),\u00a00)\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0revenue_ksh<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">FROM<\/span>\u00a0customers\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0c<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0orders\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0o<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0o.customer_id\u00a0=\u00a0c.customer_id<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.status\u00a0=\u00a0&#8216;completed&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&gt;=\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-01&#8217;<\/span>\n      <span class=\"nit-code-line\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"nit-code-keyword\">AND<\/span>\u00a0o.order_date\u00a0&lt;\u00a0<span class=\"nit-code-keyword\">DATE<\/span>\u00a0&#8216;2026-10-06&#8217;<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">LEFT<\/span>\u00a0<span class=\"nit-code-keyword\">JOIN<\/span>\u00a0order_items\u00a0<span class=\"nit-code-keyword\">AS<\/span>\u00a0oi\u00a0<span class=\"nit-code-keyword\">ON<\/span>\u00a0oi.order_id\u00a0=\u00a0o.order_id<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">GROUP<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0c.city<\/span>\n      <span class=\"nit-code-line\"><span class=\"nit-code-keyword\">ORDER<\/span>\u00a0<span class=\"nit-code-keyword\">BY<\/span>\u00a0revenue_ksh\u00a0<span class=\"nit-code-keyword\">DESC<\/span>,\u00a0c.city;<\/span>\n      <\/div>\n      <\/div>\n      <div aria-label=\"SQL lesson reference table 18\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\"><table class=\"nit-table\">\n      <thead>\n      <tr>\n      <th scope=\"col\">City<\/th>\n      <th scope=\"col\">Revenue (KSh)<\/th>\n      <\/tr>\n      <\/thead>\n      <tbody>\n      <tr>\n      <td>Kisumu<\/td>\n      <td>4,550<\/td>\n      <\/tr>\n      <tr>\n      <td>Migori<\/td>\n      <td>3,100<\/td>\n      <\/tr>\n      <tr>\n      <td>Kisii<\/td>\n      <td>250<\/td>\n      <\/tr>\n      <tr>\n      <td>Homa Bay<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <tr>\n      <td>Nairobi<\/td>\n      <td>0<\/td>\n      <\/tr>\n      <\/tbody>\n      <\/table>\n      <\/div>\n      <p><strong>Practice completion check<\/strong><\/p>\n      <p>Migori, Kisumu and Kisii together account for KSh 7,900. Zero-revenue cities remain in the output. If you get KSh 9,400, you probably included the cancelled order\u2019s KSh 1,500.<\/p>\n    <\/section>\n    <section id=\"faq\" aria-labelledby=\"faq-title\">\n      <h2 id=\"faq-title\">SQL JOIN Frequently Asked Questions<\/h2>\n      <div class=\"nit-faq\">\n        <details class=\"nit-faq__item\">\n          <summary>What is the difference between INNER JOIN and LEFT JOIN?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>INNER JOIN keeps matching row pairs only. LEFT JOIN also preserves unmatched left-table rows and fills their missing right-side columns with NULL. This is why a LEFT JOIN can show customers with zero completed orders.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>Does JOIN automatically remove duplicate rows?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>No. One customer can match several orders, and one order can match several order lines. Multiple joined rows are expected. Investigate the row grain before using DISTINCT to hide anything.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>Can I JOIN more than two SQL tables?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>Yes. The detail report joins customers, orders, order_items and products. Each JOIN needs a correct matching key and a clear reason.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>Why is my SUM larger after a JOIN?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>A one-to-many JOIN can repeat higher-level values on several rows. For example, an order total is repeated if the order is joined back to its line items. Sum at the correct grain, reconcile detail totals, and compare qualifying order-header counts before relying on the report.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>What is the difference between ON and WHERE in a LEFT JOIN?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>ON decides which right-table rows match; WHERE filters the result after the join. Filtering a right-table value in WHERE often removes otherwise-preserved unmatched left rows.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>How do I check whether a JOIN accidentally lost an order?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>Count qualifying orders directly from the <strong>orders<\/strong> table before joining. Then compare that header count with distinct order IDs in the joined report. If the counts differ, look for orders without items, restrictive filters and missing join matches. Even when totals agree, confirm the business rules and source data separately.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>Is FULL OUTER JOIN available in every database?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>No. Database engines differ in supported syntax. PostgreSQL supports FULL OUTER JOIN, as used in this lesson. Check the documentation for your chosen engine.<\/p>\n          <\/div>\n        <\/details>\n        <details class=\"nit-faq__item\">\n          <summary>What is the best way to practise SQL JOINs?<\/summary>\n          <div class=\"nit-faq__answer\">\n            <p>Begin with two small tables, predict the output, execute the JOIN, then add a third table and verify row counts. Finally compare line-level totals with GROUP BY reports, as in this lesson.<\/p>\n          <\/div>\n        <\/details>\n      <\/div>\n    <\/section>\n    <section id=\"next-skills\" aria-labelledby=\"next-skills-title\">\n      <h2 id=\"next-skills-title\">What to Learn After SQL JOINs<\/h2>\n      <p>Once you can predict INNER and LEFT JOIN outputs, practise GROUP BY, HAVING, common table expressions (CTEs), CASE, date filters, and basic window functions. A useful next project is a monthly report comparing completed-order revenue across locations.<\/p>\n      <p>For a broader analysis project, see NIT\u2019s <a href=\"https:\/\/niteducation.com\/resources\/data-analyst-projects-for-beginners-kenya\/\">seven beginner Data Analyst projects<\/a>. To understand how reporting is used at work, read <a href=\"https:\/\/niteducation.com\/resources\/what-does-a-data-analyst-do-at-work\/\">what a Data Analyst does day to day<\/a>.<\/p>\n    <\/section>\n    <section id=\"enrol\" class=\"nit-enrol\" aria-labelledby=\"enrol-title\">\n      <div class=\"nit-enrol__intro\">\n        <h2 id=\"enrol-title\">Build Real Data Skills at Newton Institute of Technology<\/h2>\n        <p>To practise these skills beyond this lesson, explore NIT\u2019s Data Science and Analytics programme. It combines SQL, Excel, Python, data cleaning and reporting; training options include Migori and online study. Confirm current admission details on the programme page.<\/p>\n      <\/div>\n      <div class=\"nit-enrol__cards\">\n        <div class=\"nit-enrol-card\"><span>SQL<\/span><strong>Practical database queries<\/strong><p>Learn to retrieve and verify business information.<\/p><\/div>\n        <div class=\"nit-enrol-card\"><span>Analysis<\/span><strong>Excel and Python<\/strong><p>Build skills for cleaning and analysing datasets.<\/p><\/div>\n        <div class=\"nit-enrol-card\"><span>Reporting<\/span><strong>Clear business insights<\/strong><p>Turn checked data into useful summaries.<\/p><\/div>\n      <\/div>\n      <div class=\"nit-enrol__actions\">\n        <a class=\"nit-button nit-button--light\" href=\"https:\/\/niteducation.com\/resources\/data-science-course-in-kenya\/\">Explore the Data Science course<\/a>\n      <\/div>\n    <\/section>\n    <section id=\"sources\" class=\"nit-sources\" aria-labelledby=\"sources-title\">\n      <h2 id=\"sources-title\">Technical References and Learning Notes<\/h2>\n      <ol>\n      <li><a href=\"https:\/\/www.postgresql.org\/docs\/current\/tutorial-join.html\" target=\"_blank\" rel=\"noopener\">PostgreSQL Documentation \u2014 Joins Between Tables<\/a>. Official JOIN fundamentals and matched\/unmatched examples.<\/li>\n      <li><a href=\"https:\/\/www.postgresql.org\/docs\/current\/queries-table-expressions.html\" target=\"_blank\" rel=\"noopener\">PostgreSQL Documentation \u2014 Table Expressions<\/a>. JOIN types, JOIN conditions, ON, WHERE and grouping semantics.<\/li>\n      <li><a href=\"https:\/\/niteducation.com\/resources\/what-is-a-database\/\">NIT \u2014 What Is a Database?<\/a>. Keys and table relationships for beginners.<\/li>\n      <li><a href=\"https:\/\/niteducation.com\/resources\/what-is-sql-for-data-analysis\/\">NIT \u2014 SQL for Data Analysis<\/a>. Introductory SQL learning pathway.<\/li>\n      <\/ol>\n      <p><strong>Dataset and accuracy:<\/strong> All customer names, transactions and monetary figures are fictional educational examples created for this lesson. The worked results were checked in SQLite with a PostgreSQL-date-literal adaptation, supplemented by adversarial mutation tests, and compared with official PostgreSQL documentation. Direct execution in PostgreSQL is still outstanding. Monetary values are in Kenyan shillings and represent completed-order line revenue only, not gross profit.<\/p>\n      <p><strong>Editorial note:<\/strong> Queries use PostgreSQL syntax. Adapt status, payment, refund and tax rules to the actual database and business before using this approach operationally.<\/p>\n    <\/section>\n  <\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>NIT ICT LESSON \u00b7 SQL \u00b7 BEGINNER TO PRACTICAL SQL JOINs Explained: Build a Sales Report Step by Step Learn INNER, LEFT, RIGHT and FULL OUTER JOINs using four linked sales tables. Run each query, see the result, find wrong totals and verify a five-day report. Build the report Practise SQL customers-orders.sql SELECT c.customer_name, &#160;&#160;&#160;&#160;&#160;&#160; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[30],"tags":[163,910,911],"class_list":["post-3949","post","type-post","status-publish","format-standard","hentry","category-ict","tag-plant-operator-colleges-in-kenya","tag-sql-joins-explained","tag-sql-joins-with-examples"],"_links":{"self":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts\/3949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/comments?post=3949"}],"version-history":[{"count":1,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts\/3949\/revisions"}],"predecessor-version":[{"id":3950,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts\/3949\/revisions\/3950"}],"wp:attachment":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/media?parent=3949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/categories?post=3949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/tags?post=3949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}