{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "'Connected: @None'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%load_ext sql\n", "%sql sqlite://" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * sqlite://\n", "Done.\n", "Done.\n", "1 rows affected.\n", "1 rows affected.\n", "1 rows affected.\n", "1 rows affected.\n" ] }, { "data": { "text/plain": [ "[]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql drop table if exists product;\n", "create table product(\n", " pname varchar primary key, -- product 이름\n", " price money, -- product의 가격\n", " category varchar, -- category\n", " manufacturer varchar NOT NULL -- manufacturer\n", ");\n", "insert into product values('Gizmo', 19.99, 'Gadgets', 'GizmoWorks');\n", "insert into product values('PowerGizmo', 29.99, 'Gadgets', 'GizmoWorks');\n", "insert into product values('MultiTouch', 203.99, 'Household', 'Hitachi');\n", "insert into product values('SingleTouch', 149.99, 'Photography', 'Canon');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "활동 02b:\n", "-------------\n", "\n", "단일 테이블 질의문" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "문제 #1\n", "-----------\n", "\n", "이름에 \"Touch\"가 포함되는 모든 결과를 출력하는 질의문을 작성해보자. 결과에는 이름과 가격을 출력하되 제조사의 이름순으로 정렬해보자. \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "products 를 먼저 살펴보자:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * sqlite://\n", "Done.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pnamepricecategorymanufacturer
Gizmo19.99GadgetsGizmoWorks
PowerGizmo29.99GadgetsGizmoWorks
MultiTouch203.99HouseholdHitachi
SingleTouch149.99PhotographyCanon
" ], "text/plain": [ "[('Gizmo', 19.99, 'Gadgets', 'GizmoWorks'),\n", " ('PowerGizmo', 29.99, 'Gadgets', 'GizmoWorks'),\n", " ('MultiTouch', 203.99, 'Household', 'Hitachi'),\n", " ('SingleTouch', 149.99, 'Photography', 'Canon')]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%sql select * from product;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "아래에 질의문을 작성해보자:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * sqlite://\n" ] }, { "data": { "text/plain": [ "'Connected: @None'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%sql \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "다음에는 \"Gizmo\"라는 이름이 포함된 제품을 파는 제조사의 이름을 중복없이 출력하는 질의문을 작성해보자:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * sqlite://\n" ] }, { "data": { "text/plain": [ "'Connected: @None'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%sql \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "문제 #2:\n", "------------\n", "\n", "다음은 정렬에 대한 질의문들이다. \n", "아래의 질의문들을 실행하기 전에 결과를 예상해보자. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%sql SELECT DISTINCT category FROM product ORDER BY category;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%sql SELECT category FROM product ORDER BY pname;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%sql SELECT DISTINCT category FROM product ORDER BY pname;" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }