SQL
-
[HackerRank] Top CompetitorsSQL 2021. 9. 28. 00:00
https://www.hackerrank.com/challenges/full-score/problem Top Competitors | HackerRank Query a list of top-scoring hackers. www.hackerrank.com Code select s.hacker_id, h.name from submissions s, difficulty d, challenges c, hackers h where d.difficulty_level = c.difficulty_level and s.challenge_id = c.challenge_id and h.hacker_id = s.hacker_id and s.score = d.score group by s.hacker_id, h.name hav..
-
[HackerRank] Binary Tree NodesSQL 2021. 9. 23. 23:30
https://www.hackerrank.com/challenges/binary-search-tree-1/problem Binary Tree Nodes | HackerRank Write a query to find the node type of BST ordered by the value of the node. www.hackerrank.com Code 1 SELECT N, (CASE WHEN P IS NULL THEN "Root" WHEN N IN (SELECT P FROM BST) THEN "Inner" ELSE "Leaf" END) as R FROM BST ORDER BY N; Code 2 SELECT N, IF(P IS NULL, 'Root', IF(B.N IN (SELECT P FROM BST)..
-
[HackerRank] Weather Observation Station 5SQL 2021. 9. 23. 22:41
https://www.hackerrank.com/challenges/weather-observation-station-5/problem Weather Observation Station 5 | HackerRank Write a query to print the shortest and longest length city name along with the length of the city names. www.hackerrank.com Code 1 (SELECT City, Length(City) FROM STATION ORDER BY LENGTH(City), City ASC LIMIT 1) UNION (SELECT City, Length(City) FROM STATION ORDER BY LENGTH(City..
-
[필기] DB - SQL쿼리 - ORMSQL 2021. 7. 29. 15:40
얄팍한 SQL쿼리문과 그보단 조금 나은 파이썬을 통해 DB를 생성, 관리, 데이터를 뽑아오는 법을 배우는 중이다. 귀도 아저씨의 PEP249 설명 - PEP249 Documentation: https://www.python.org/dev/peps/pep-0249/ sqlite3 or psycopg2 DB와 python을 연결해주는 패키지. SQL쿼리문 사용. SQLAlchemy: ORM DB와 프로그래밍언어를 연결해주는 중간다리. SQL쿼리문없이 프로그래밍언어 사용. 1. sqlite3 - > sqlite 연결 등 documentation: https://docs.python.org/ko/3/library/sqlite3.html sqlite3 — SQLite 데이터베이스용 DB-API 2.0 인터페이스 ..
-
[sqlalchemy] 테이블간 relationship 설정SQL 2021. 7. 26. 14:58
테이블간 one to many등과 같은 관계가 있을때, sqlslachemy의 relationship을 통해서 해당 관계를 지정할 수 있다. 테이블을 생성할 때 추가해주면 된다. 공식문서: https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html Relationship 설정 1 - #backref 사용하기 relationship('Class명', backref ='해당테이블 명') class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) screen_name = Column(String) tweets = relationship('Tweet', backref='use..
-
[MySQL-일기장] COUNT(*) 는 NULL을 포함한다SQL 2021. 7. 22. 22:48
2021. 07. 22 SQL 일기 > COUNT(*) COUNT(*) 함수는 NULL VALUE를 포함하여 계산한다. 너무 당연한 얘긴데 문제 풀때마다 잊는 듯 하다. > GROUPBY(Col) GROUP BY {column명} 후에 Count(*) 하는 경우에는 Null값은 Null값대로 그룹이 생성될 터이니, Null은 제거하고 Count 해줄 것이다. (아니 물론 HAVING 에 VALUE를 지정해줘야겠지만...) > 프로그래머스 코딩테스트 연습 > GROUP BY > 고양이와 개는 몇마리 있을까 문제를 잘 읽자. 결과 값은 같을지언정 나는 틀렸다. 동물 보호소에 들어온 동물 중 고양이와 개가 각각 몇 마리인지 조회하는 SQL문을 작성해주세요. 이때 고양이를 개보다 먼저 조회해주세요. -- 틀린답..