SQL

·SQL/MySQL
문제설명 Table: Logs +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ id is the primary key for this table. Write an SQL query to find all numbers that appear at least three times consecutively. Return the result table in any order. The query result format is in the following example. Example 1: Input: Logs table: +--..
·SQL/MySQL
You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks. Grades contains the following data: Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades are entered first. ..
·SQL/MySQL
with 문 MySQL에서는 WITH문으로 가상 테이블을 만들 수 있는데 작성방법은 다음과 같다. WITH 가상테이블 명 AS ( SELECT ~~ FROM ~~~ 등등 )이러한 방법은 전 글의 https://sunho99.tistory.com/entry/MySQL-Subquery%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EC%8B%AC%ED%99%94%EB%AC%B8%EC%A0%9C-Hacker-Rank-Challenges%ED%92%80%EC%96%B4%EB%B3%B4%EA%B8%B0 에 해당하는 문제를 풀때도 사용 할 수 있다. WITH counter AS( SELECT hackers.hacker_id, hackers.name, COUNT(*) AS challenges_c..
·SQL/MySQL
문제 https://www.hackerrank.com/challenges/challenges/problem?h_r=internal-search 문제가 길다보니 링크로 대체하겠다. 문제 풀이 SELECT hackers.hacker_id, hackers.name, COUNT(*) AS challenges_created FROM Challenges INNER JOIN hackers ON Challenges.hacker_id = Hackers.hacker_id GROUP BY hackers.hacker_id, hackers.name HAVING challenges_created = (SELECT MAX(challenges_created) FROM( SELECT hacker_id, COUNT(*) AS chall..
·SQL/MySQL
Department Highest Salary 문제 We define an employee's total earnings to be their monthly salary x months worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as space-separate..
·SQL/MySQL
Subquery의 개념 subquery란 하나의 SQL문 안에 포함되어 있는 또 다른 SQL문을 말한다. 서브쿼리는 메인쿼리가 서브쿼리를 포함하는 종속적인 관계이다. 서브쿼리를 사용 할 때 주의점 서브쿼리를 괄호로 감싸서 사용한다. 서브쿼리에서는 ORDER BY를 사용하지 못한다. 서브쿼리는 단일 행 또는 복수 행 비교 연산자와 함께 사용 가능하다. FROM 절 서브쿼리 가상의 테이블을 만든다 생각하면 편하다. 동적으로 생선된 테이블이라 생각하면 된다. 임시적인 뷰이기 때문에 데이터베이스에 저장되지는 않는다. ex) SELECT daily_stats.week , AVG (daily_stats.incidents_daily FROM ( SELECT week, date, count (incident_id) A..
·SQL/MySQL
문제1 LeetCode 627. [Swap-Salary] https://leetcode.com/problems/swap-salary/ 사용된 개념 UPDATE 테이블명 SET 컬럼명 = CASE WHEN condition1 THEN value_if_condition1_true WEHN condition2 THEN value_if_condition2_true ELSE value_other_case END (WHERE 조건식); 문제 풀이 UPDATE salary SET sex = CASE WHEN SEX ='m' THEN 'f' WHEN SEX ='f' THEN 'm' END; 문제2 LeetCode 196. [Delete Duplicate emai..
·SQL/MySQL
INSERT 1. 테이블 전체에 데이터 추가하는 방법 INSERT INTO 테이블명 VALUES (VALUE_LIST) -> 컬럼 순서대로 입력 예시 Salary id Name Salary Date INSERT INTO Salary VALUES ('1','A','250','2020-03-31'); id Name Salary Date 1 A 250 2020-03-31 2. 값을 저장할 열 지정하기 INSERT INTO 테이블명 (COLUMN LIST) VALUES (VALUE_LIST); 예시 Salary id Name Salary Date 1 A 250 2020-03-31 INSERT INTO Salary Id,Salary VALUES ('..
·SQL/MySQL
문제 You are given a table, Functions, containing two columns: X and Y. COLUMN TYPE X Integer Y Interger Two pairs (X1, Y1) and (X2, Y2) are said to be symmetric pairs if X1 = Y2 and X2 = Y1. Write a query to output all such symmetric pairs in ascending order by the value of X. List the rows such that X1 ≤ Y1. Sample Input X Y 20 20 20 20 20 21 23 22 22 23 21 20 Sample Output 20 20 20 21 22 23문제 코..
Shine_sunho
'SQL' 카테고리의 글 목록 (2 Page)