Bird
0
0

Which SQL statement correctly creates this view?

hard📝 Application Q15 of 15
SQL - Views
You want to create a view RecentOrders that always shows orders placed in the last 7 days from the Orders table with columns order_id, customer_id, and order_date. Which SQL statement correctly creates this view?
ACREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
BCREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
CCREATE VIEW RecentOrders AS SELECT * FROM Orders WHERE order_date > DATEADD(day, -7, GETDATE());
DCREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > SYSDATE - 7;
Step-by-Step Solution
Solution:
  1. Step 1: Identify needed columns and date filter

    The view should include order_id, customer_id, and order_date columns and filter orders from last 7 days.
  2. Step 2: Check each option's correctness

    CREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; misses order_date column, so it won't show the date. CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; includes all needed columns and uses correct standard SQL interval syntax. CREATE VIEW RecentOrders AS SELECT * FROM Orders WHERE order_date > DATEADD(day, -7, GETDATE()); uses SQL Server syntax (DATEADD, GETDATE) which may not be standard. CREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > SYSDATE - 7; uses SYSDATE which is Oracle-specific and may not work in standard SQL.
  3. Final Answer:

    CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; -> Option A
  4. Quick Check:

    Include needed columns and use standard interval syntax [OK]
Quick Trick: Include all needed columns and use standard date intervals [OK]
Common Mistakes:
MISTAKES
  • Omitting important columns in view
  • Using non-standard date functions
  • Forgetting to filter by date correctly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes