Introduction to SQL for Data Analysis 📈

¡

3 min read

The what why and how of SQL when you are just getting started.

What is SQL?

SQL (Structured Query Language) is a programming language designed for managing data in a relational database.

Why SQL?

  • SQL is the “meat and potatoes” of data analysis
  • It’s used for accessing, cleaning, and analyzing data that’s stored in databases
  • It’s semantically easy to understand and learn.
  • Because it can be used to access large amounts of data directly where it’s stored, analysts don’t have to copy data into other applications.
  • Compared to spreadsheet tools, data analysis done in SQL is easy to audit and replicate.

SELECT and FROM

  • SELECT indicates which columns you’d like to view, and FROM identifies the table that they are a part of.

SELECT Colname1,Colname2, Colname3...
FROM Tablename

  • If you want to select every column in a table, you can use * instead of the column names
  • The syntax in SQL is not case sensitive.
  • To format the way table names are printed using SELECT :
SELECT existingcolname AS wantedcolname,  
  FROM Tablename

Note : SELECT does NOT change any data in the database, it can only be used to fetch and print data.

LIMIT

  • LIMIT is used as a simple way to keep their queries from taking too long to return.
  • If the aim is to just look at data then first few are enough, no point in printing millions of rows because the query will take too long to give results.
SELECT Colname1,Colname2, Colname3...  
FROM Tablename  
LIMIT number

ORDER BY

  • ORDER BY is used when the result of a query are to be presented in an orderly fashion ( rating of products from highest to lowest ).
SELECT *  
FROM tablename  
ORDER BY colname
  • Orders the results in increasing order of column colname. The results are printed in ascending order by default, to print in descending order DESC has to be specified after each column in the clause.
  • Example: ORDER BY colname DESC
  • Ordering can also be done with multiple columns when the results need to be ordered but every category needs to be preserved.
SELECT *     
FROM tablename    
ORDER BY colname1, colname2

Comments

  • Comments are used to enhance the readability of code. When written between code lines the interpreter identifies them and does not execute them. Comments can span across one or multiple lines.
  • Single line comment : You can use — — (two dashes) to comment out everything to the right of them on a given line
SELECT *  --This comment won't affect the way the code runs  
FROM tablename
  • Multi line Comment : Comments across multiple lines use /* to begin the comment and */ to close.
SELECT *  /* Here's a comment so long and descriptive that  
it could only fit on multiple lines. Fortunately,  
it, too, will not affect how this code runs. */  
FROM tablename

WHERE

  • WHERE is used to filter the data with the help of a condition utilizing the comparison and logical operators.
SELECT Colname1,Colname2, Colname3...  
FROM Tablename  
WHERE condition  
LIMIT number

That’s it for today, in the next part we’ll discuss about the various operators available in SQL that help filter data. Stay tuned!

Link to the next part :

[Operators in SQL for Data Analysis 📊
Operators are essential for data wrangling and filtering.medium.com](https://medium.com/@aakriti.sharma18/operators-in-sql-for-data-analysis-7296ab5103f9 "medium.com/@aakriti.sharma18/operators-in-s..")

Â