Skip to content

Commit 20436da

Browse files
committed
license and joins added
1 parent 931bf03 commit 20436da

File tree

5 files changed

+252
-1
lines changed

5 files changed

+252
-1
lines changed

Joins/Examples.sql

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
-- INNER JOIN syntax and example
3+
4+
SELECT column_name(s)
5+
FROM table1
6+
INNER JOIN table2
7+
ON table1.column_name = table2.column_name;
8+
9+
-- Example
10+
11+
SELECT Orders.OrderID, Customers.ContactName, Orders.OrderDate
12+
FROM Orders
13+
INNER JOIN Customers
14+
ON Orders.CustomerID = Customers.CustomerID;
15+
16+
-- This query retrieves Order ID, Customer's contact name and Order Date by matching CustomerID in Orders and Customers tables.
17+
18+
-- Order ID ContactName Phone OrderDate
19+
-- 10249 Karin Josephs 0251-031259 1996-07-05 00:00:00.000
20+
21+
-- Example with 3 tables
22+
23+
SELECT Orders.OrderID, Customers.ContactName, [Orders Details].ProductID, Orders.OrderDate
24+
FROM ((Orders
25+
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
26+
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID);
27+
28+
-- This query retrieves Order ID, Customer's contact name, Order Product ID and Order Date by matching CustomerID in Orders and Customer tables and matching OrderID in Orders and Order Details tables.
29+
30+
-- Order ID ContactName ProductID OrderDate
31+
-- 10294 Paula Wilson 1 1996-08-30 00:00:00.000
32+
33+
34+
-- LEFT JOIN syntax and example
35+
36+
SELECT column_name(s)
37+
FROM table1
38+
LEFT JOIN table2
39+
ON table1.column_name = table2.column_name;
40+
41+
-- Example
42+
43+
SELECT Orders.OrderID, Customers.ContactName
44+
FROM Orders
45+
LEFT JOIN Customers
46+
ON Orders.CustomerID = Customers.CustomerID
47+
ORDER BY Customers.ContactName;
48+
49+
-- This query retrieves all Order ID and their corresponding Customer's contact name including orders with no associated customer.
50+
51+
-- Order ID ContactName
52+
-- 10281 Alejandra Camino
53+
54+
55+
-- RIGHT JOIN syntax and example
56+
57+
SELECT column_name(s)
58+
FROM table1
59+
RIGHT JOIN table2
60+
ON table1.column_name = table2.column_name;
61+
62+
-- Example
63+
64+
SELECT Orders.OrderID, Customers.CompanyName
65+
FROM Orders
66+
RIGHT JOIN Customers
67+
ON Orders.CustomerID = Customers.CustomerID;
68+
69+
-- This query retrieves all Company Name and their corresponding Order IDs including customers with no associated orders.
70+
71+
-- Order ID CompanyName
72+
-- 10643 Alfreds Futterkiste
73+
74+
75+
-- FULL OUTER JOIN syntax and example
76+
77+
SELECT column_name(s)
78+
FROM table1
79+
FULL OUTER JOIN table2
80+
ON table1.column_name = table2.column_name;
81+
82+
-- Example
83+
84+
SELECT Orders.OrderID, Customers.CompanyName, Customers.ContactName
85+
FROM Orders
86+
FULL OUTER JOIN Customers
87+
ON Orders.CustomerID = Customers.CustomerID
88+
ORDER BY Customers.ContactName;
89+
90+
-- This query retrieves all Order IDs, Customer Company Names and Contact Names including orders with no associated customer and customers with no associated orders.
91+
92+
-- Order ID CompanyName ContactName
93+
-- 10281 Romero y tomillo Alejandra Camino
94+
95+
96+
-- SELF JOIN syntax and example
97+
98+
SELECT column_name(s)
99+
FROM table1 AS t1
100+
JOIN table1 AS t2
101+
ON t1.column_name = t2.column_name;
102+
103+
-- Example (this example has done without db sample)
104+
105+
SELECT e1.name as employee, e2.name as manager
106+
FROM Employees as e1
107+
INNER JOIN Employees as e2
108+
ON e1.ManagerID = e2.EmployeeID;
109+
110+
-- This query retrieves Employee names and their corresponding manager names by joining the employees table with itself.
111+
112+
-- employee manager
113+
-- Ali Mehmet Doruk Çoralı
114+
115+
116+
-- CROSS JOIN syntax and example
117+
118+
SELECT column_name(s)
119+
FROM table1
120+
CROSS JOIN table2;
121+
122+
-- Example
123+
124+
SELECT Products.ProductID, Products.ProductName, Categories.CategoryID, Categories.CategoryName
125+
FROM Products
126+
CROSS JOIN Categories;
127+
128+
-- This query retrieves all possible combinations of products IDs and names and categories IDs and names.
129+
130+
-- ProductID ProductName CategoryID CategoryName
131+
-- 1 Chai 1 Beverages

Joins/HackerRank.sql

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Problem: Placements -> https://www.hackerrank.com/challenges/placements/problem?isFullScreen=false
2+
3+
-- You are given three tables: Students, Friends and Packages. Students contains two columns: ID and Name. Friends contains two columns: ID and Friend_ID (ID of the ONLY best friend). Packages contains two columns: ID and Salary (offered salary in $ thousands per month).
4+
-- Write a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer.
5+
6+
-- Explanation
7+
8+
-- Samantha's best friend got offered a higher salary than her at 11.55
9+
-- Julia's best friend got offered a higher salary than her at 12.12
10+
-- Scarlet's best friend got offered a higher salary than her at 15.2
11+
-- Ashley's best friend did NOT get offered a higher salary than her
12+
13+
-- The name output, when ordered by the salary offered to their friends, will be:
14+
-- Samantha
15+
-- Julia
16+
-- Scarlet
17+
18+
-- My Solution
19+
20+
SELECT Students.Name
21+
FROM ((Students
22+
INNER JOIN Friends ON Students.ID = Friends.ID)
23+
INNER JOIN Packages as Friend_Packages ON Friend_Packages.ID = Friends.Friend_ID)
24+
WHERE Friend_Packages.Salary > (
25+
SELECT Student_Packages.Salary
26+
FROM Packages as Student_Packages
27+
WHERE Student_Packages.ID = Students.ID
28+
)
29+
ORDER BY Friend_Packages.Salary;
30+
31+
32+
-- Problem: Symmetric Pairs -> https://www.hackerrank.com/challenges/symmetric-pairs/problem?isFullScreen=false
33+
34+
-- You are given a table, Functions, containing two columns: X and Y.
35+
-- Two pairs (X1, Y1) and (X2, Y2) are said to be symmetric pairs if X1 = Y2 and X2 = Y1.
36+
-- Write a query to output all such symmetric pairs in ascending order by the value of X. List the rows such that X1 ≤ Y1.
37+
38+
-- My Solution
39+
40+
SELECT f1.X, f1.Y
41+
FROM Functions as f1
42+
INNER JOIN Functions as f2
43+
ON f1.X = f2.Y and f2.X = f1.Y
44+
GROUP BY f1.X, f1.Y
45+
HAVING COUNT(f1.X) > 1 or f1.X < f1.Y
46+
ORDER BY f1.X;

Joins/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# JOINS
2+
3+
A JOIN used to combine rows from two or more tables, based on a related column between them.
4+
5+
6+
## Different Types of Joins
7+
8+
- **INNER JOIN:** Return records that have matching values in both tables.
9+
- **LEFT JOIN:** Return records of the left table and matching records in the right table.
10+
- **RIGHT JOIN:** Return records of the right table and matching records in the left table.
11+
- **FULL OUTER JOIN:** Return matching records of the left and the right table.
12+
- **SELF JOIN:** A self join is a unique type of join where a table is joined with itself.
13+
- **CROSS JOIN:** A cross join, also known as a Cartesian join, returns the Cartesian product of the two tables, meaning each row from the first table is combined with each row from the second table.
14+
<br />
15+
![img1](https://miro.medium.com/v2/resize:fit:1400/0*rFMChX4SAmQ9RzF9.png)

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 nidea1
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
11
# SQL Examples
22

3-
I will do functions, transactions, triggers, views and stored procedures with SQL in this repo.
3+
I will implement functions, transactions, triggers, views, stored procedures and other Advanced SQL Topics in this repo.
4+
5+
Database [sample](https://github.com/microsoft/sql-server-samples/blob/master/samples/databases/northwind-pubs/instnwnd.sql) used for in this repo examples.
6+
7+
## What is SQL?
8+
9+
Structured Query Language (**SQL**) is programming language for storing and processing data in a relational database.
10+
11+
## Using DB Sample
12+
13+
```sql
14+
15+
-- Firstly create a new database
16+
CREATE DATABASE ExampleDatabase
17+
GO
18+
-- After use new database for db sample
19+
USE ExampleDatabase
20+
GO
21+
22+
-- Now copy database sample here ↓
23+
24+
/*
25+
** Copyright Microsoft, Inc. 1994 - 2000
26+
** All Rights Reserved.
27+
*/
28+
29+
-- This script does not create a database.
30+
-- Run this script in the database you want the objects to be created.
31+
-- Default schema is dbo.
32+
33+
SET NOCOUNT ON
34+
GO
35+
36+
set quoted_identifier on
37+
GO
38+
.
39+
.
40+
.
41+
```

0 commit comments

Comments
 (0)