Sunday, March 24, 2019

SQL Joins



Database Normalization: 

  • Are the tables storing logical groupings of the data?
  • Can I make changes in a single location, rather than in many tables for the same information?
  • Can I access and manipulate data quickly and efficiently?
Join: 
Working with multiple tables at once.


SELECT orders.*
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;

Entity Relationship Diagrams (ERD)



















- Primary Key(PK): A primary key exists in every table, and it is a column that has a unique value for every row. This is the first column in each of our tables. IT is common that the primary key is the first column in our tables in most databases. 

- Foreign Key(FK): A foreign key is when we see a primary key in another table. We can see these in the previous ERD the foreign keys as below: 
account_id
sales_rep_id
region_id

Alias
FROM tablename1 AS t1
JOIN tablename2 AS t2

select a.primary_poc, a.name, b.channel, b.occurred_at
from accounts as a 
join web_events as b
on a.id = b.account_id
where a.name = 'Walmart'

SQL Joins

Database Normalization:  Are the tables storing logical groupings of the data? Can I make changes in a single location, rather than in...