{"id":2819,"date":"2016-04-05T14:03:22","date_gmt":"2016-04-05T14:03:22","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=2819"},"modified":"2023-01-10T06:48:33","modified_gmt":"2023-01-10T06:48:33","slug":"sql-sample-ms-sql-database-with-a-table","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/sql-sample-ms-sql-database-with-a-table\/","title":{"rendered":"SQL Sample: MS SQL Database with a Table"},"content":{"rendered":"<p>MS SQL TASK:<\/p>\n<p>Create new MS SQL database. In this database, create a table with the following information:<\/p>\n<p>John Smith (age 31): cashier<br \/>\nMary Sue (age 42): cashier<br \/>\nMike Presley (age 35): shop assistant<br \/>\nBill Walker (age 29): guard<br \/>\nKate Davis (age 26): manager<!--more--><\/p>\n<p>Execute the following SQL queries and provide the results of their execution in form of screenshots:<\/p>\n<p>-Display the total count of table entrances<br \/>\n-Display all worker positions from this table<br \/>\n-Display all rows from this table which correspond to workers over 30 years of age<br \/>\n-Display the total count of table entrances which correspond to workers under 40 years of age. This time try to give the result a meaningful name.<\/p>\n<p>SOLUTION:<\/p>\n<p>In this sample we are going to create a basic MS SQL database and table in order to execute several queries. We will use the Management Studio distributed with MS SQL Server 2014. Once installed and started, SQL Server Management Studio will greet the user with \u201cConnect to Server\u201d window, where based on the setup configuration the user will be able access Windows Authentication (which we will use for the sake of this example) and alternatively, SQL Server Authentication (which requires inputting login and password).<\/p>\n<p>After connecting to server, we can see Object Explorer window on the left side of SQL Server Management Studio. By opening Databases window we can see the list of existing databases. If no user databases were created, we will only see System Databases (master, model, msdb, tempdb). We can create a new database by executing a new query. A new query tab can be opened by either clicking on the &#8220;New Query&#8221; icon or pressing Ctrl+N. The query text should look similarly to this:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql.png\" rel=\"attachment wp-att-2821\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2821 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql.png\" alt=\"sql\" width=\"565\" height=\"582\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql.png 565w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-291x300.png 291w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><\/p>\n<p>The parameters used (such as filename and filesize) are subjective to the user and can be modified.<\/p>\n<p>A query can be executed by either clicking on the \u201cExecute&#8221; icon or pressing F5. It results in:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-1.png\" rel=\"attachment wp-att-2823\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2823 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-1.png\" alt=\"sql sample_screenshot 1\" width=\"584\" height=\"525\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-1.png 584w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-1-300x270.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p>We can notice that while the new database was created, its existence is not reflected in Object Explorer. In order to fix this, we should refresh Object Explorer window by pressing Refresh icon or pressing F5 when Object Explorer window is active.<\/p>\n<p>In order for our next queries to interact with this database, we should mark it as the one currently used:<\/p>\n<p>USE Sample<\/p>\n<p>Result:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-2.png\" rel=\"attachment wp-att-2825\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2825 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-2.png\" alt=\"sql sample_screenshot 2\" width=\"548\" height=\"210\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-2.png 548w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-2-300x115.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/a><\/p>\n<p>Next, we should create a new table called \u201cWorkers\u201d. The command should also specify the names and types (int for storing digits, char for storing characters\/strings) of table&#8217;s rows. First row will be used as unique row identifier (id), therefore marked as PRIMARY KEY. Additionally, since we do not plan any of these rows to contain no data, we will mark every of them as NOT NULL. This results in the following query:<\/p>\n<p>CREATE TABLE Workers<br \/>\n(<br \/>\nworker_id int NOT NULL PRIMARY KEY,<br \/>\nworker_name char(50) NOT NULL,<br \/>\nworker_age int NOT NULL,<br \/>\nworker_position char(50) NOT NULL<br \/>\n)<\/p>\n<p>Results:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-3.png\" rel=\"attachment wp-att-2827\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2827 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-3.png\" alt=\"sql sample_screenshot 3\" width=\"610\" height=\"227\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-3.png 610w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-3-300x112.png 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>The rows should be filled with appropriate data:<\/p>\n<p>INSERT INTO Workers(worker_id,worker_name,worker_age,worker_position)<br \/>\nVALUES(1,&#8217;John Smith&#8217;,31,&#8217;cashier&#8217;)<br \/>\nINSERT INTO Workers(worker_id,worker_name,worker_age,worker_position)<br \/>\nVALUES(2,&#8217;Mary Sue&#8217;,42,&#8217;cashier&#8217;)<br \/>\nINSERT INTO Workers(worker_id,worker_name,worker_age,worker_position)<br \/>\nVALUES(3,&#8217;Mike Presley&#8217;,35,&#8217;shop assistant&#8217;)<br \/>\nINSERT INTO Workers(worker_id,worker_name,worker_age,worker_position)<br \/>\nVALUES(4,&#8217;Bill Walker&#8217;,29,&#8217;guard&#8217;)<br \/>\nINSERT INTO Workers(worker_id,worker_name,worker_age,worker_position)<br \/>\nVALUES(5,&#8217;Kate Davis&#8217;,26,&#8217;manager&#8217;)<\/p>\n<p>Results:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-4.png\" rel=\"attachment wp-att-2829\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2829\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-4.png\" alt=\"sql sample_screenshot 4\" width=\"600\" height=\"302\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-4.png 798w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-4-300x151.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-4-768x387.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>At this point, our table is created and filled with data, which allows us to start working on first out of four required queries, \u201cDisplay the total count of table entrances\u201d :<\/p>\n<p>SELECT COUNT(*)<br \/>\nFROM Workers<\/p>\n<p>Result:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-5.png\" rel=\"attachment wp-att-2831\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2831 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-5.png\" alt=\"sql sample_screenshot 5\" width=\"252\" height=\"175\" \/><\/a><\/p>\n<p>Second query, \u201cDisplay all worker positions from this table\u201d, requires us to show the data based on specific column:<\/p>\n<p>SELECT worker_position<\/p>\n<p>FROM Workers<\/p>\n<p>Result:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-6.png\" rel=\"attachment wp-att-2833\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2833 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-6.png\" alt=\"sql sample_screenshot 6\" width=\"251\" height=\"234\" \/><\/a><\/p>\n<p>Third query, \u201cDisplay all rows from this table which correspond to workers over 30 years of age\u201d requires us to introduce a limit based on data in certain column:<\/p>\n<p>SELECT *<br \/>\nFROM Workers<br \/>\nWHERE worker_age &gt; 30<\/p>\n<p>Result:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-7.png\" rel=\"attachment wp-att-2835\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2835 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-7.png\" alt=\"sql sample_screenshot 7\" width=\"353\" height=\"218\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-7.png 353w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-7-300x185.png 300w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/a><\/p>\n<p>The last query is \u201cDisplay the total count of table entrances which correspond to workers under 40 years of age. This time try to give the result a meaningful name.\u201d<br \/>\nIt is a combination of approaches used in several previos queries, with the only new element being the addition of alias to the resulting column:<\/p>\n<p>SELECT COUNT(*) AS workers_under_40<\/p>\n<p>FROM Workers<\/p>\n<p>WHERE worker_age &lt; 40<\/p>\n<p>Result:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-8.png\" rel=\"attachment wp-att-2837\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2837 size-full\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/03\/sql-sample_screenshot-8.png\" alt=\"sql sample_screenshot 8\" width=\"290\" height=\"178\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>SQL Assistance from the Experts<\/h2>\n<blockquote><p><em>The posted SQL sample was completed by one of AssignmentShark.com writers. If you can&#8217;t cope with your technical projects, we will provide <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">online assignment help<\/a> you to solve any of the issues. Just specify your instructions and receive a completed assignment within a specified deadline.<\/em><\/p>\n<p><em>If you are truly interested in completing IT assignments on your own, you can read one of another <a href=\"https:\/\/assignmentshark.com\/blog\/sql-code-samples-database\/\" target=\"_blank\" rel=\"noopener noreferrer\">SQL code samples on database<\/a>. Or ask for <a href=\"https:\/\/assignmentshark.com\/sql-homework-help.html\" target=\"_blank\" rel=\"noopener\">help with SQL homework<\/a> one of our experts.<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>MS SQL TASK: Create new MS SQL database. In this database, create a table with the following information: John Smith (age 31): cashier Mary Sue (age 42): cashier Mike Presley (age 35): shop assistant Bill Walker (age 29): guard Kate Davis (age 26): manager<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-2819","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/2819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=2819"}],"version-history":[{"count":13,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/2819\/revisions"}],"predecessor-version":[{"id":13563,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/2819\/revisions\/13563"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=2819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=2819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=2819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}