-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelect.sql
More file actions
162 lines (116 loc) · 5.11 KB
/
Select.sql
File metadata and controls
162 lines (116 loc) · 5.11 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- Create Books Table
CREATE TABLE books (
id serial primary key,
title varchar(100) not null,
author varchar(100) not null,
isbn bigint unique,
category varchar(50),
price numeric(8,2),
pages int,
rating decimal(2,1),
stock smallint,
is_available boolean,
published_date date,
tags text[],
metadata jsonb
);
-- Insert Book Records
INSERT INTO books (
title, author, isbn, category, price, pages, rating, stock, is_available, published_date, tags, metadata
)
VALUES
('Clean Code', 'Robert Martin', 1001, 'Programming', 850.00, 464, 4.8, 10, true, '2008-08-01',
ARRAY['coding','best-practice'],
'{"publisher":"Prentice Hall","language":"EN","format":"hardcover"}'),
('The Pragmatic Programmer', 'Andrew Hunt', 1002, 'Programming', 950.00, 352, 4.9, 8, true, '1999-10-20',
ARRAY['software','development'],
'{"publisher":"Addison-Wesley","language":"EN","format":"paperback"}'),
('Learning SQL', 'Alan Beaulieu', 1003, 'Database', 700.00, 338, 4.5, 5, true, '2020-04-10',
ARRAY['sql','database'],
'{"publisher":"OReilly","language":"EN","format":"paperback"}'),
('PostgreSQL Basics', 'John Smith', 1004, 'Database', 650.00, 280, 4.3, 7, true, '2021-02-01',
ARRAY['postgresql','backend'],
'{"publisher":"TechPress","language":"EN","format":"ebook"}'),
('Python Crash Course', 'Eric Matthes', 1005, 'Programming', 900.00, 544, 4.8, 6, true, '2019-05-03',
ARRAY['python','beginner'],
'{"publisher":"No Starch","language":"EN","format":"hardcover"}'),
('Data Structures', 'Mark Allen', 1006, 'Computer Science', 750.00, 420, 4.2, 4, false, '2018-01-01',
ARRAY['algorithms','ds'],
'{"publisher":"CSBooks","language":"EN","format":"paperback"}'),
('Algorithms', 'Thomas Cormen', 1007, 'Computer Science', 820.00, 240, 4.7, 9, true, '2013-03-01',
ARRAY['algorithms','complexity'],
'{"publisher":"MIT Press","language":"EN","format":"hardcover"}'),
('HTML and CSS', 'Jon Duckett', 1008, 'Web', 600.00, 490, 4.6, 12, true, '2011-11-08',
ARRAY['html','css'],
'{"publisher":"Wiley","language":"EN","format":"paperback"}'),
('Eloquent JavaScript', 'Marijn Haverbeke', 1009, 'Programming', 780.00, 472, 4.6, 8, true, '2018-12-04',
ARRAY['javascript','frontend'],
'{"publisher":"No Starch","language":"EN","format":"ebook"}'),
('React Guide', 'Sarah Green', 1010, 'Web', 880.00, 390, 4.5, 5, true, '2022-01-15',
ARRAY['react','frontend'],
'{"publisher":"WebPress","language":"EN","format":"ebook"}'),
('Book 11', 'Author 11', 1011, 'Fiction', 500.00, 200, 4.0, 3, true, '2022-02-01',
ARRAY['novel'],
'{"publisher":"ABC","language":"EN","format":"paperback"}'),
('Book 12', 'Author 12', 1012, 'Fiction', 520.00, 220, 4.1, 4, true, '2022-03-01',
ARRAY['story'],
'{"publisher":"ABC","language":"EN","format":"paperback"}'),
('Book 13', 'Author 13', 1013, 'History', 530.00, 230, 4.2, 2, false, '2022-04-01',
ARRAY['history'],
'{"publisher":"XYZ","language":"EN","format":"hardcover"}'),
('Book 14', 'Author 14', 1014, 'History', 540.00, 240, 4.3, 6, true, '2022-05-01',
ARRAY['world-history'],
'{"publisher":"XYZ","language":"EN","format":"paperback"}'),
('Book 15', 'Author 15', 1015, 'Science', 550.00, 250, 4.4, 7, true, '2022-06-01',
ARRAY['science'],
'{"publisher":"TechPub","language":"EN","format":"ebook"}'),
('Book 16', 'Author 16', 1016, 'Science', 560.00, 260, 4.5, 8, true, '2022-07-01',
ARRAY['research'],
'{"publisher":"TechPub","language":"EN","format":"ebook"}'),
('Book 17', 'Author 17', 1017, 'Math', 570.00, 270, 4.6, 1, false, '2022-08-01',
ARRAY['math'],
'{"publisher":"MathWorld","language":"EN","format":"paperback"}'),
('Book 18', 'Author 18', 1018, 'Math', 580.00, 280, 4.7, 9, true, '2022-09-01',
ARRAY['algebra'],
'{"publisher":"MathWorld","language":"EN","format":"hardcover"}'),
('Book 19', 'Author 19', 1019, 'Database', 590.00, 290, 4.8, 10, true, '2022-10-01',
ARRAY['postgresql','sql'],
'{"publisher":"DBPub","language":"EN","format":"ebook"}'),
('Book 20', 'Author 20', 1020, 'Web', 600.00, 300, 4.9, 11, true, '2022-11-01',
ARRAY['web','frontend'],
'{"publisher":"WebPub","language":"EN","format":"ebook"}');
-- Select All Books
select * from books
-- Using Column Alias
select title as "Title of Books", price as "Price of Books" from books
-- Using Multiple Column Aliases
-- Sort Books by Price (Descending)
select * from books
order by price desc
-- Select Unique Categories (Distinct)
select distinct category from books
-- Filter Books Using WHERE Clause
-- Filter Using Equal (=) Operator
select * from books
where category='Math'
-- Select Available Books
select * from books
where is_available = true
-- Filter Using OR Operator
select * from books
where category='Math' or category='Fiction'
-- Filter Using AND, OR and Comparison Operators
select * from books
where (category='Math' or category='Fiction' or category='Programming') and (price >500)
-- Filter Using BETWEEN Operator
select * from books
where pages between 200 and 350
-- Filter Using IN Operator
select * from books
where category in ('Math','Programming')
-- Filter from Array
select * from books
where 'coding' = any(tags)
-- Filter from Jsonb
select * from books
where metadata->>'format'='ebook'