-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics-syntax-comparision
More file actions
33 lines (25 loc) · 899 Bytes
/
Copy pathbasics-syntax-comparision
File metadata and controls
33 lines (25 loc) · 899 Bytes
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
# 1. SELECT
SELECT emp_id, emp_name, salary FROM employees;
df.select("emp_id", "emp_name", "salary")
--------------------------------------------------
# 2. WHERE / FILTER
SELECT * FROM employees WHERE salary > 50000;
from pyspark.sql.functions import col
df.filter(col("salary") > 50000)
-------------------------------------------------
# 3. Multiple Conditions
SELECT * FROM employees WHERE salary > 50000 AND age > 25;
df.filter((col("salary") > 50000) & (col("age") > 25))
Interview Question:
Why use & and not and?
-> and is Python logical operator
-> & works on Spark Column objects
-------------------------------------------------
# 4. ORDER BY
SELECT * FROM employees ORDER BY salary DESC;
df.orderBy(col("salary").desc())
--------------------------------------------------
#5. LIMIT
SELECT * FROM employees LIMIT 5;
df.limit(5)
--------------------------------------------------