/sql-coding-challenge

Repository for coding challenge with SQL

Primary LanguageRuby

Coding challenges

The goal of this repository is to improve SQL programming skills with a coding challenge. The implementation of these challenges follows the methodology of Test-Driven Development, i.e.:

  • Think about tests cases
  • Write the tests
  • Implement the code
  • Test
  • Refactoring
  • Test again until all tests pass

The challenges are based on Codewars exercises. The SQL engine is PostgreSQL 9.6 and the tests cases are based on Behavior Driven Development of Ruby/RSpec SQL.

  1. Trimming the Field
  2. Simple Having
  3. Count Weekdays
  4. Members Approved for Voucher
  5. Hierarchical Structure
  6. Calculating Running Total
  7. Present XML data to SQL way
  8. Top 10 customer by total payments amount
  9. Analyzing the sales by product and date
  10. SQL with Harry Potter: Sorting Hat Comparators
  11. Top Departments
  12. Conditional Count
  13. Monsters using CASE
  14. Elven Wildcards
  15. Group by day
  16. Age Calculator
  17. Ranking Sales
  18. Maths with String Manipulation
  19. Regex Extraction
  20. Filtering Sales
  21. Using Window Functions to Get Top N per Group
  22. Find all Movies that Two Actors Cast in Together
  23. Simple table totaling
  24. Regex AlphaNumeric Split
  25. Pivoting data
  26. Returning Strings
  27. Handling with Nulls

Useful notes about RSpec SQL

A small set of utility methods and classes have been provided to make testing and displaying SQL queries easy with Ruby. These utility methods are available from any file in the runner.

  • $sql is a global variable which contains the user's SQL solution
  • run_sql is a method that will run the user's query, execute any non-SELECT statements and print and return any result sets from SQL SELECT statements.
  • compare_with is a method which compares the candidate's submitted query to the reference query and auto-generates a test suite based on the expected query results.