[Reference] 초보자를 위한 SQL 200제


[SQL 문법]

데이터가 할당되지 않은 상태인 NULL 값을 검색할 수 있다.

SELECT ename, comm
  FROM emp
 WHERE comm is null

[예시]

# 오라클 연동 및 접속
import pandas as pd
import cx_Oracle
dsn=cx_Oracle.makedsn('localhost',1521,'orcl')
db=cx_Oracle.connect('scott','tiger')
cursor=db.cursor()

# SQL 문법
cursor.execute("""
SELECT ename, comm
  FROM emp
 WHERE comm is null
""")

row=cursor.fetchall()
colname=cursor.description
col=[]

for i in colname:
    col.append(i[0])

# pandas를 사용한 데이터 프레임 형식으로 변환
emp=pd.DataFrame(row,columns=col)
print(emp)

[결과]

    ENAME  COMM
0    KING  None
1   BLAKE  None
2   CLARK  None
3   JONES  None
4   JAMES  None
5    FORD  None
6   SMITH  None
7   SCOTT  None
8   ADAMS  None
9  MILLER  None

Leave a comment