Data Science Hub
  • Data Science Hub
  • STATISTICS
    • Introduction
    • Fundamentals
      • Data Types
      • Central Tendency, Asymmetry, and Variability
      • Sampling
      • Confidence Interval
      • Hypothesis Testing
    • Distributions
      • Exponential Distribution
    • A/B Testing
      • Sample Size Calculation
      • Multiple Testing
  • Database
    • Database Fundamentals
    • Database Management Systems
    • Data Warehouse vs Data Lake
  • SQL
    • SQL Basics
      • Creating and Modifying Tables/Views
      • Data Types
      • Joins
    • SQL Rules
    • SQL Aggregate Functions
    • SQL Window Functions
    • SQL Data Manipulation
      • String Operations
      • Date/Time Operations
    • SQL Descriptive Stats
    • SQL Tips
    • SQL Performance Tuning
    • SQL Customization
    • SQL Practice
      • Designing Databases
        • Spotify Database Design
      • Most Commonly Asked
      • Mixed Queries
      • Popular Websites For SQL Practice
        • SQLZoo
          • World - BBC Tables
            • SUM and COUNT Tutorial
            • SELECT within SELECT Tutorial
            • SELECT from WORLD Tutorial
            • Select Quiz
            • BBC QUIZ
            • Nested SELECT Quiz
            • SUM and COUNT Quiz
          • Nobel Table
            • SELECT from Nobel Tutorial
            • Nobel Quiz
          • Soccer / Football Tables
            • JOIN Tutorial
            • JOIN Quiz
          • Movie / Actor / Casting Tables
            • More JOIN Operations Tutorial
            • JOIN Quiz 2
          • Teacher - Dept Tables
            • Using Null Quiz
          • Edinburgh Buses Table
            • Self join Quiz
        • HackerRank
          • SQL (Basic)
            • Select All
            • Select By ID
            • Japanese Cities' Attributes
            • Revising the Select Query I
            • Revising the Select Query II
            • Revising Aggregations - The Count Function
            • Revising Aggregations - The Sum Function
            • Revising Aggregations - Averages
            • Average Population
            • Japan Population
            • Population Density Difference
            • Population Census
            • African Cities
            • Average Population of Each Continent
            • Weather Observation Station 1
            • Weather Observation Station 2
            • Weather Observation Station 3
            • Weather Observation Station 4
            • Weather Observation Station 6
            • Weather Observation Station 7
            • Weather Observation Station 8
            • Weather Observation Station 9
            • Weather Observation Station 10
            • Weather Observation Station 11
            • Weather Observation Station 12
            • Weather Observation Station 13
            • Weather Observation Station 14
            • Weather Observation Station 15
            • Weather Observation Station 16
            • Weather Observation Station 17
            • Weather Observation Station 18
            • Weather Observation Station 19
            • Higher Than 75 Marks
            • Employee Names
            • Employee Salaries
            • The Blunder
            • Top Earners
            • Type of Triangle
            • The PADS
          • SQL (Intermediate)
            • Weather Observation Station 5
            • Weather Observation Station 20
            • New Companies
            • The Report
            • Top Competitors
            • Ollivander's Inventory
            • Challenges
            • Contest Leaderboard
            • SQL Project Planning
            • Placements
            • Symmetric Pairs
            • Binary Tree Nodes
            • Interviews
            • Occupations
          • SQL (Advanced)
            • Draw The Triangle 1
            • Draw The Triangle 2
            • Print Prime Numbers
            • 15 Days of Learning SQL
          • TABLES
            • City - Country
            • Station
            • Hackers - Submissions
            • Students
            • Employee - Employees
            • Occupations
            • Triangles
        • StrataScratch
          • Netflix
            • Oscar Nominees Table
            • Nominee Filmography Table
            • Nominee Information Table
          • Audible
            • Easy - Audible
          • Spotify
            • Worldwide Daily Song Ranking Table
            • Billboard Top 100 Year End Table
            • Daily Rankings 2017 US
          • Google
            • Easy - Google
            • Medium - Google
            • Hard - Google
        • LeetCode
          • Easy
  • Python
    • Basics
      • Variables and DataTypes
        • Lists
        • Dictionaries
      • Control Flow
      • Functions
    • Object Oriented Programming
      • Restaurant Modeler
    • Pythonic Resources
    • Projects
  • Machine Learning
    • Fundamentals
      • Supervised Learning
        • Classification Algorithms
          • k-Nearest Neighbors
            • kNN Parameters & Attributes
          • Logistic Regression
        • Classification Report
      • UnSupervised Learning
        • Clustering
          • Evaluation
      • Preprocessing
        • Scalers: Standard vs MinMax
        • Feature Selection vs Dimensionality Reduction
        • Encoding
    • Frameworks
    • Machine Learning in Advertising
    • Natural Language Processing
      • Stopwords
      • Name Entity Recognition (NER)
      • Sentiment Analysis
        • Agoda Reviews - Part I - Scraping Reviews, Detecting Languages, and Preprocessing
        • Agoda Reviews - Part II - Sentiment Analysis and WordClouds
    • Recommendation Systems
      • Spotify Recommender System - Artists
  • Geospatial Analysis
    • Geospatial Analysis Basics
    • GSA at Work
      • Web Scraping and Mapping
  • GIT
    • GIT Essentials
    • Connecting to GitHub
  • FAQ
    • Statistics
  • Cloud Computing
    • Introduction to Cloud Computing
    • Google Cloud Platform
  • Docker
    • What is Docker?
Powered by GitBook
On this page
  • A. Statement Rules
  • B. Comments
  • C. Aliases
  • D. Naming of the Columns/Tables/Queries
  • E. Query Clause order

Was this helpful?

  1. SQL

SQL Rules

A. Statement Rules

  • Cases: SQL statements are not case-sensitive, meaning that "SELECT" is considered the same as "select" or "Select". A common practice is to write SQL keywords in uppercase and use lowercase for column and table names, which makes code easier to read and debug.

  • White Spaces: All extra white space within a SQL statement is ignored when that statement is processed. SQL statements can be specified on one long line or broken up over many lines. So, the following three statements are functionally identical:

  • Termination: If running multiple SQL statements, those statements must be separated by a "; (semicolon)" , at the end of each statment.MO

SELECT title FROM Movies;

select title
from Movies;

Select
title
From
movies;

B. Comments

  • For multiline comments, use /* your comment */ (though it is rare, it can also be used inline commenting see examples below)

  • For inline/entire-line comments, use -- (two hyphens). It is a good practice (and a style requirement for certain DBMS) for readibility to have at least one whitespace after the second dash.

/* MULTILINE COMMENT
comment line 1
comment line 2
comment line 3 
*/
SELECT title FROM Movies;


-- INLINE COMMENT
SELECT title -- inline comment
FROM Movies;
-- OR
SELECT 
    title, 
--    release_year, 
    director
FROM Movies;
-- OR
SELECT title, /*release_year,*/ director
FROM Movies;


-- ENTIRE LINE COMMENT
-- SELECT title, release_year, director
SELECT title, release_year, director
FROM Movies;

C. Aliases

Aliases can be used to specify columns (or combination of columns), tables, and queries and are of great help when:

  • Column names are either too long or not readable

  • Calculations are performed on columns or functions are used to combine columns

  • There are more than one table involved in a query

In contrast to column aliases, table aliases are never returned to the client.

Use of AS Keyword:

Though the use of AS keyword is optional, it helps for easy reading.

-- aliases for column names
SELECT actor_date_of_birth AS dob
FROM Actors;

-- aliases for calculated fields
SELECT AVG(budget) avg_budget -- without AS keyword
FROM Movies;

-- aliases for combined columns
SELECT CONCAT(first_name,' ',last_name) AS full_name
FROM Actor;

-- aliases for table names 
SELECT 
    a.first_name, 
    a.last_name, 
    m.title, 
    m.release_year
FROM Actors AS a, Movies AS m
WHERE a.movie_id = m.id
    AND m.release_year > 2000;

Notice the use of table aliases with column names. This is particularly useful to easily track the sources of fields and mandatory when there are columns with the same name in tables that are joined.

D. Naming of the Columns/Tables/Queries

Do not use Keywords or Clauses to name columns, tables, or queries

When naming columns (or tables) try to use explicit words and avoid shortening words that can only be understood by a handful of people (if not only by you) as much as possible.

It is also good practice to create column names as a chain of words and separate them with underscore "_", as opposed to using whitespaces. However, if you must/prefer to use spaces to separate the words then you need to use double quotes ' " ' around the column name

-- aliases for calculated fields
SELECT AVG(budget) AS "Avearage Budget"
FROM Movies;

Note: This is really the only place in which you'll ever want to use double quotes in SQL. Single quotes for everything else.

E. Query Clause order

The order in which the clauses are executed

  1. SELECT

  2. FROM

  3. WHERE

  4. GROUP BY

  5. HAVING

  6. ORDER BY

Last updated 7 months ago

Was this helpful?