DEV Community

SQL 101:INTRODUCTION TO SQL FOR DATA ANALYSIS

Tony Ochieng on March 01, 2023

What is SQL? SQL (Structured Query Language) is a powerful programming language that is widely used for managing and manipulating data in a relatio...
Collapse
 
aarone4 profile image
Aaron Reese

Good article. Well done. A couple of minor points...
1) inline comments using the -- can be risky. If you save the code and it gets reloaded without the line breaks you could end up changing the logic of the query. E.g. if you commented out and AND subclause in the WHERE clause.
2) in 30 years of coding I have never come across a legitimate use of RIGHT JOIN in production code. It is a code-smell that you don't understand the data. E.g. if you wanted all orders and their customer details, then refactor the code to select orders first and LEFT JOIN to customers.
3) in MSSQL the INNER and FULL keywords are redundant. LEFT and RIGHT are not.
4) you neglected CROSS JOIN. This is a more complex setup so it may have been deliberate as this is an intro post but thought it was worth mentioning
5) although you gave a good overview if keys and relationships you didn't mention Third Normal Form which is important for keys to be effective. In you SELECT examples you join customer to order and get product details and price. In a 3NF design you are unlikely to hold product details on the order and there could be a 1:many relationship (orderLine.order_id = order.id) and order value should not be stored if it can be calculated from summing the order lines as the two values can get out of sync.

But overall a good overview of basic SQL