FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SQL/join.sql at main · Sakin790/SQL · GitHub
Sakin790
/
SQL
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SQL
/
join.sql
Copy path
More file actions
More file actions
Latest commit
History
History
History
107 lines (84 loc) · 2.77 KB
Breadcrumbs
SQL
/
join.sql
Copy path
File metadata and controls
107 lines (84 loc) · 2.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--
Active: 1753857534394@@127.0.0.1@3306@JOINS
CREATE
DATABASE
JOINS
DEFAULT CHARACTER
SET
=
'
utf8mb4
'
;
use JOINS;
CREATE
TABLE
students
(
id
INT
PRIMARY KEY
,
name
VARCHAR
(
50
)
);
CREATE
TABLE
scores
(
student_id
INT
,
score
INT
,
FOREIGN KEY
(student_id)
REFERENCES
students(id)
);
INSERT INTO
students (id, name)
VALUES
(
1
,
'
Alice
'
),
(
2
,
'
Bob
'
),
(
3
,
'
Charlie
'
),
(
4
,
'
David
'
);
INSERT INTO
scores (student_id, score)
VALUES
(
1
,
85
),
(
3
,
92
),
(
4
,
78
);
SELECT
*
from
scores;
--
----------------------------------------INNER JOIN--------------------------
SELECT
columnName
FROM
tableA
AS
t1
INNER JOIN
tableB
AS
t2
ON
t1
.
commonColumnName
=
t2
.
commonColumnName
;
SELECT
tableOne
.
id
,
tableOne
.
name
,
tableTwo
.
score
FROM
students
AS
tableOne
INNER JOIN
scores
AS
tableTwo
ON
tableOne
.
id
=
tableTwo
.
student_id
;
--
----Aug 10--------------------------
use Learning;
SELECT
*
from
employees;
CREATE
TABLE
departments
(
department_id
INT
PRIMARY KEY
AUTO_INCREMENT,
department_name
VARCHAR
(
50
)
NOT NULL
UNIQUE,
location
VARCHAR
(
50
),
manager_name
VARCHAR
(
100
),
budget
DECIMAL
(
12
,
2
),
established_year YEAR
);
INSERT INTO
departments (department_name, location, manager_name, budget, established_year)
VALUES
(
'
HR
'
,
'
Germany
'
,
'
Laura Becker
'
,
500000
.
00
,
2010
),
(
'
Sales
'
,
'
USA
'
,
'
John Smith
'
,
1200000
.
00
,
2008
),
(
'
Finance
'
,
'
India
'
,
'
Priya Sharma
'
,
900000
.
00
,
2012
),
(
'
Marketing
'
,
'
Bangladesh
'
,
'
Rafiq Ahmed
'
,
750000
.
00
,
2015
),
(
'
Engineering
'
,
'
Canada
'
,
'
Alice Johnson
'
,
2000000
.
00
,
2005
);
SELECT
*
FROM
departments;
use Learning;
SELECT
*
from
employees;
CREATE
TABLE
departments
(
department_id
INT
PRIMARY KEY
AUTO_INCREMENT,
department_name
VARCHAR
(
50
)
NOT NULL
UNIQUE,
location
VARCHAR
(
50
),
manager_name
VARCHAR
(
100
),
budget
DECIMAL
(
12
,
2
),
established_year YEAR
);
INSERT INTO
departments (department_name, location, manager_name, budget, established_year)
VALUES
(
'
HR
'
,
'
Germany
'
,
'
Laura Becker
'
,
500000
.
00
,
2010
),
(
'
Sales
'
,
'
USA
'
,
'
John Smith
'
,
1200000
.
00
,
2008
),
(
'
Finance
'
,
'
India
'
,
'
Priya Sharma
'
,
900000
.
00
,
2012
),
(
'
Marketing
'
,
'
Bangladesh
'
,
'
Rafiq Ahmed
'
,
750000
.
00
,
2015
),
(
'
Engineering
'
,
'
Canada
'
,
'
Alice Johnson
'
,
2000000
.
00
,
2005
);
use Learning;
SELECT
*
FROM
departments;
SELECT
*
FROM
employees;
--
-add relation in existing table
ALTER
TABLE
employees
ADD COLUMN department_id
INT
;
ALTER
TABLE
employees
FOREIGN KEY
(department_id)
REFERENCES
departments(department_id);
--
------
--
1. Add the department_id column to employees table (if it doesn't exist yet)
ALTER
TABLE
employees
ADD COLUMN department_id
INT
;
--
2. Add foreign key constraint to relate employees.department_id to departments.department_id
ALTER
TABLE
employees
ADD
CONSTRAINT
fk_department
FOREIGN KEY
(department_id)
REFERENCES
departments(department_id);
Back
|
FazBrowse Home
|
New Git URL