Anda di halaman 1dari 5

SQL Interview Questions with Answers

http://www.sqlauthority.com
What is RD !S" Relational Data ase !ana#ement Systems $RD !S% are &ata'ase mana#ement systems that maintain &ata recor&s an& in&ices in ta'les. Relationships may 'e create& an& maintaine& across an& amon# the &ata an& ta'les. In a relational &ata'ase( relationships 'etween &ata items are e)presse& 'y means o* ta'les. Inter&epen&encies amon# these ta'les are e)presse& 'y &ata values rather than 'y pointers. +his allows a hi#h &e#ree o* &ata in&epen&ence. An RD !S has the capa'ility to recom'ine the &ata items *rom &i**erent *iles( provi&in# power*ul tools *or &ata usa#e. What is normali,ation" Data'ase normali,ation is a &ata &esi#n an& or#ani,ation process applie& to &ata structures 'ase& on rules that help 'uil& relational &ata'ases. In relational &ata'ase &esi#n( the process o* or#ani,in# &ata to minimi,e re&un&ancy. -ormali,ation usually involves &ivi&in# a &ata'ase into two or more ta'les an& &e*inin# relationships 'etween the ta'les. +he o'.ective is to isolate &ata so that a&&itions( &eletions( an& mo&i*ications o* a *iel& can 'e ma&e in .ust one ta'le an& then propa#ate& throu#h the rest o* the &ata'ase via the &e*ine& relationships. What are &i**erent normali,ation *orms" /-0: 1liminate Repeatin# 2roups !a3e a separate ta'le *or each set o* relate& attri'utes( an& #ive each ta'le a primary 3ey. 1ach *iel& contains at most one value *rom its attri'ute &omain. 4-0: 1liminate Re&un&ant Data I* an attri'ute &epen&s on only part o* a multi5value& 3ey( remove it to a separate ta'le. 6-0: 1liminate 7olumns -ot Depen&ent 8n 9ey I* attri'utes &o not contri'ute to a &escription o* the 3ey( remove them to a separate ta'le. All attri'utes must 'e &irectly &epen&ent on the primary 3ey 7-0: oyce57o&& -ormal 0orm I* there are non5trivial &epen&encies 'etween can&i&ate 3ey attri'utes( separate them out into &istinct ta'les. :-0: Isolate In&epen&ent !ultiple Relationships -o ta'le may contain two or more /:n or n:m relationships that are not &irectly relate&. ;-0: Isolate Semantically Relate& !ultiple Relationships +here may 'e practical constrains on in*ormation that .usti*y separatin# lo#ically relate& many5to5many relationships. 8-0: 8ptimal -ormal 0orm A mo&el limite& to only simple $elemental% *acts( as e)presse& in 8'.ect Role !o&el notation. D9-0: Domain59ey -ormal 0orm A mo&el *ree *rom all mo&i*ication anomalies. Remem'er( these normali,ation #ui&elines are cumulative. 0or a &ata'ase to 'e in 6-0( it must *irst *ul*ill all the criteria o* a 4-0 an& /-0 &ata'ase. What is Store& <roce&ure" A store& proce&ure is a name& #roup o* SQL statements that have 'een previously create& an& store& in the server &ata'ase. Store& proce&ures accept input parameters so that a sin#le proce&ure can 'e use& over the networ3 'y several clients usin# &i**erent input &ata. An& when the proce&ure is mo&i*ie&( all clients automatically #et the new version. Store& proce&ures re&uce networ3 tra**ic an& improve per*ormance. Store& proce&ures can 'e use& to help ensure the inte#rity o* the &ata'ase. e.#. sp=help&'( sp=rename&'( sp=&epen&s etc. What is +ri##er" A tri##er is a SQL proce&ure that initiates an action when an event $I-S1R+( D1L1+1 or ><DA+1% occurs. +ri##ers are store& in an& mana#e& 'y the D !S.+ri##ers are use& to maintain the re*erential http://www.sqlauthority.com

http://www.sqlauthority.com

http://www.sqlauthority.com inte#rity o* &ata 'y chan#in# the &ata in a systematic *ashion. A tri##er cannot 'e calle& or e)ecute&? the D !S automatically *ires the tri##er as a result o* a &ata mo&i*ication to the associate& ta'le. +ri##ers can 'e viewe& as similar to store& proce&ures in that 'oth consist o* proce&ural lo#ic that is store& at the &ata'ase level. Store& proce&ures( however( are not event5&rive an& are not attache& to a speci*ic ta'le as tri##ers are. Store& proce&ures are e)plicitly e)ecute& 'y invo3in# a 7ALL to the proce&ure while tri##ers are implicitly e)ecute&. In a&&ition( tri##ers can also e)ecute store& proce&ures. -este& +ri##er: A tri##er can also contain I-S1R+( ><DA+1 an& D1L1+1 lo#ic within itsel*( so when the tri##er is *ire& 'ecause o* &ata mo&i*ication it can also cause another &ata mo&i*ication( there'y *irin# another tri##er. A tri##er that contains &ata mo&i*ication lo#ic within itsel* is calle& a neste& tri##er. What is @iew" A simple view can 'e thou#ht o* as a su'set o* a ta'le. It can 'e use& *or retrievin# &ata( as well as up&atin# or &eletin# rows. Rows up&ate& or &elete& in the view are up&ate& or &elete& in the ta'le the view was create& with. It shoul& also 'e note& that as &ata in the ori#inal ta'le chan#es( so &oes &ata in the view( as views are the way to loo3 at part o* the ori#inal ta'le. +he results o* usin# a view are not permanently store& in the &ata'ase. +he &ata accesse& throu#h a view is actually constructe& usin# stan&ar& +5SQL select comman& an& can come *rom one to many &i**erent 'ase ta'les or even other views. What is In&e)" An in&e) is a physical structure containin# pointers to the &ata. In&ices are create& in an e)istin# ta'le to locate rows more quic3ly an& e**iciently. It is possi'le to create an in&e) on one or more columns o* a ta'le( an& each in&e) is #iven a name. +he users cannot see the in&e)es( they are .ust use& to spee& up queries. 1**ective in&e)es are one o* the 'est ways to improve per*ormance in a &ata'ase application. A ta'le scan happens when there is no in&e) availa'le to help a query. In a ta'le scan SQL Server e)amines every row in the ta'le to satis*y the query results. +a'le scans are sometimes unavoi&a'le( 'ut on lar#e ta'les( scans have a terri*ic impact on per*ormance. 7lustere& in&e)es &e*ine the physical sortin# o* a &ata'ase ta'leAs rows in the stora#e me&ia. 0or this reason( each &ata'ase ta'le may have only one clustere& in&e). -on5clustere& in&e)es are create& outsi&e o* the &ata'ase ta'le an& contain a sorte& list o* re*erences to the ta'le itsel*. What is the &i**erence 'etween clustere& an& a non5clustere& in&e)" A clustere& in&e) is a special type o* in&e) that reor&ers the way recor&s in the ta'le are physically store&. +here*ore ta'le can have only one clustere& in&e). +he lea* no&es o* a clustere& in&e) contain the &ata pa#es. A nonclustere& in&e) is a special type o* in&e) in which the lo#ical or&er o* the in&e) &oes not match the physical store& or&er o* the rows on &is3. +he lea* no&e o* a nonclustere& in&e) &oes not consist o* the &ata pa#es. Instea&( the lea* no&es contain in&e) rows. What are the &i**erent in&e) con*i#urations a ta'le can have" A ta'le can have one o* the *ollowin# in&e) con*i#urations: -o in&e)es A clustere& in&e) A clustere& in&e) an& many nonclustere& in&e)es A nonclustere& in&e) !any nonclustere& in&e)es What is cursors" 7ursor is a &ata'ase o'.ect use& 'y applications to manipulate &ata in a set on a row5'y5row 'asis( instea& o* the typical SQL comman&s that operate on all the rows in the set at one time. http://www.sqlauthority.com

http://www.sqlauthority.com In or&er to wor3 with a cursor we nee& to per*orm some steps in the *ollowin# or&er: Declare cursor 8pen cursor 0etch row *rom the cursor <rocess *etche& row 7lose cursor Deallocate cursor What is the use o* D 77 comman&s" D 77 stan&s *or &ata'ase consistency chec3er. We use these comman&s to chec3 the consistency o* the &ata'ases( i.e.( maintenance( vali&ation tas3 an& status chec3s. 1.#. D 77 7B179D 5 1nsures that ta'les in the &' an& the in&e)es are correctly lin3e&. D 77 7B179ALL87 5 +o chec3 that all pa#es in a &' are correctly allocate&. D 77 7B1790IL12R8>< 5 7hec3s all ta'les *ile #roup *or any &ama#e. What is a Lin3e& Server" Lin3e& Servers is a concept in SQL Server 'y which we can a&& other SQL Server to a 2roup an& query 'oth the SQL Server &'s usin# +5SQL Statements. With a lin3e& server( you can create very clean( easy to *ollow( SQL statements that allow remote &ata to 'e retrieve&( .oine& an& com'ine& with local &ata. Storpe& <roce&ure sp=a&&lin3e&server( sp=a&&lin3e&srvlo#in will 'e use& a&& new Lin3e& Server. What is 7ollation" 7ollation re*ers to a set o* rules that &etermine how &ata is sorte& an& compare&. 7haracter &ata is sorte& usin# rules that &e*ine the correct character sequence( with options *or speci*yin# case5 sensitivity( accent mar3s( 3ana character types an& character wi&th. What are &i**erent type o* 7ollation Sensitivity" 7ase sensitivity A an& a( an& '( etc. Accent sensitivity a an& C( o an& D( etc. 9ana Sensitivity When Eapanese 3ana characters Bira#ana an& 9ata3ana are treate& &i**erently( it is calle& 9ana sensitive. Wi&th sensitivity When a sin#le5'yte character $hal*5wi&th% an& the same character when represente& as a &ou'le5'yte character $*ull5wi&th% are treate& &i**erently then it is wi&th sensitive. WhatFs the &i**erence 'etween a primary 3ey an& a unique 3ey" oth primary 3ey an& unique en*orce uniqueness o* the column on which they are &e*ine&. ut 'y &e*ault primary 3ey creates a clustere& in&e) on the column( where are unique creates a nonclustere& in&e) 'y &e*ault. Another ma.or &i**erence is that( primary 3ey &oesnFt allow ->LLs( 'ut unique 3ey allows one ->LL only. Bow to implement one5to5one( one5to5many an& many5to5many relationships while &esi#nin# ta'les" 8ne5to58ne relationship can 'e implemente& as a sin#le ta'le an& rarely as two ta'les with primary an& *orei#n 3ey relationships. 8ne5to5!any relationships are implemente& 'y splittin# the &ata into two ta'les with primary 3ey an& *orei#n 3ey relationships. !any5to5!any relationships are implemente& usin# a .unction ta'le with the 3eys *rom 'oth the ta'les *ormin# the composite primary 3ey o* the .unction ta'le. What is a -8L879" http://www.sqlauthority.com

http://www.sqlauthority.com >sin# the -8L879 query optimiser hint is #enerally consi&ere& #oo& practice in or&er to improve concurrency on a 'usy system. When the -8L879 hint is inclu&e& in a S1L17+ statement( no loc3s are ta3en when &ata is rea&. +he result is a Dirty Rea&( which means that another process coul& 'e up&atin# the &ata at the e)act time you are rea&in# it. +here are no #uarantees that your query will
retrieve the most recent &ata. +he a&vanta#e to per*ormance is that your rea&in# o* &ata will not 'loc3

up&ates *rom ta3in# place( an& up&ates will not 'loc3 your rea&in# o* &ata. S1L17+ statements ta3e Share& $Rea&% loc3s. +his means that multiple S1L17+ statements are allowe& simultaneous access( 'ut other processes are 'loc3e& *rom mo&i*yin# the &ata. +he up&ates will queue until all the rea&s have complete&( an& rea&s requeste& a*ter the up&ate will wait *or the up&ates to complete. +he result to your system is &elay$'loc3in#%. What is &i**erence 'etween D1L1+1 G +R>-7A+1 comman&s" Delete comman& removes the rows *rom a ta'le 'ase& on the con&ition that we provi&e with a WB1R1 clause. +runcate will actually remove all the rows *rom a ta'le an& there will 'e no &ata in the ta'le a*ter we run the truncate comman&. +R>-7A+1 +R>-7A+1 is *aster an& uses *ewer system an& transaction lo# resources than D1L1+1. +R>-7A+1 removes the &ata 'y &eallocatin# the &ata pa#es use& to store the ta'leAs &ata( an& only the pa#e &eallocations are recor&e& in the transaction lo#. +R>-7A+1 removes all rows *rom a ta'le( 'ut the ta'le structure an& its columns( constraints( in&e)es an& so on remain. +he counter use& 'y an i&entity *or new rows is reset to the see& *or the column. Hou cannot use +R>-7A+1 +A L1 on a ta'le re*erence& 'y a 08R1I2- 91H constraint. ecause +R>-7A+1 +A L1 is not lo##e&( it cannot activate a tri##er. +R>-7A+1 can not 'e Rolle& 'ac3. +R>-7A+1 is DDL 7omman&. +R>-7A+1 Resets i&entity o* the ta'le. D1L1+1 D1L1+1 removes rows one at a time an& recor&s an entry in the transaction lo# *or each &elete& row. I* you want to retain the i&entity counter( use D1L1+1 instea&. I* you want to remove ta'le &e*inition an& its &ata( use the DR8< +A L1 statement. D1L1+1 7an 'e use& with or without a WB1R1 clause D1L1+1 Activates +ri##ers. D1L1+1 7an 'e Rolle& 'ac3. D1L1+1 is D!L 7omman&. D1L1+1 &oes not reset i&entity o* the ta'le. Di**erence 'etween 0unction an& Store& <roce&ure" >D0 can 'e use& in the SQL statements anywhere in the WB1R1/BA@I-2/S1L17+ section where as Store& proce&ures cannot 'e. >D0s that return ta'les can 'e treate& as another rowset. +his can 'e use& in E8I-s with other ta'les. Inline >D0Fs can 'e thou#h o* as views that ta3e parameters an& can 'e use& in E8I-s an& other Rowset operations. When is the use o* ><DA+1=S+A+IS+I7S comman&" +his comman& is 'asically use& when a lar#e processin# o* &ata has occurre&. I* a lar#e amount o* &eletions any mo&i*ication or ul3 7opy into the ta'les has occurre&( it has to up&ate the in&e)es to ta3e these chan#es into account. ><DA+1=S+A+IS+I7S up&ates the in&e)es on these ta'les accor&in#ly. What types o* Eoins are possi'le with Sql Server" Eoins are use& in queries to e)plain how &i**erent ta'les are relate&. Eoins also let you select &ata *rom a ta'le &epen&in# upon &ata *rom another ta'le. +ypes o* .oins: I--1R E8I-s( 8>+1R E8I-s( 7R8SS E8I-s. 8>+1R E8I-s are *urther classi*ie& as L10+ 8>+1R E8I-S( RI2B+ 8>+1R E8I-S an& 0>LL 8>+1R E8I-S. http://www.sqlauthority.com

http://www.sqlauthority.com What is the &i**erence 'etween a BA@I-2 7LA>S1 an& a WB1R1 7LA>S1" Speci*ies a search con&ition *or a #roup or an a##re#ate. BA@I-2 can 'e use& only with the S1L17+ statement. BA@I-2 is typically use& in a 2R8>< H cluse. When 2R8>< H is not use&( BA@I-2 'ehaves li3e a WB1R1 clause. Bavin# 7lause is 'asically use& only with the 2R8>< H *unction in a query. WB1R1 7lause is applie& to each row 'e*ore they are part o* the 2R8>< H *unction in a query. What is su'5query" 1)plain properties o* su'5query. Su'5queries are o*ten re*erre& to as su'5selects( as they allow a SELECT statement to 'e e)ecute& ar'itrarily within the 'o&y o* another SQL statement. A su'5query is e)ecute& 'y enclosin# it in a set o* parentheses. Su'5queries are #enerally use& to return a sin#le row as an atomic value( thou#h they may 'e use& to compare values a#ainst multiple rows with the IN 3eywor&. A su'query is a S1L17+ statement that is neste& within another +5SQL statement. A su'query S1L17+ statement i* e)ecute& in&epen&ently o* the +5SQL statement( in which it is neste&( will return a result set. !eanin# a su'query S1L17+ statement can stan&alone an& is not &epen&e& on the statement in which it is neste&. A su'query S1L17+ statement can return any num'er o* values( an& can 'e *oun& in( the column list o* a S1L17+ statement( a 0R8!( 2R8>< H( BA@I-2( an&/or 8RD1R H clauses o* a +5SQL statement. A Su'query can also 'e use& as a parameter to a *unction call. asically a su'query can 'e use& anywhere an e)pression can 'e use&. <roperties o* Su'5Query A su'query must 'e enclose& in the parenthesis. A su'query must 'e put in the ri#ht han& o* the comparison operator( an& A su'query cannot contain a 8RD1R5 H clause. A query can contain more than one su'5queries. What are types o* su'5queries" Sin#le5row su'query( where the su'query returns only one row. !ultiple5row su'query( where the su'query returns multiple rows(.an& !ultiple column su'query( where the su'query returns multiple columns. What is SQL <ro*iler" SQL <ro*iler is a #raphical tool that allows system a&ministrators to monitor events in an instance o* !icroso*t SQL Server. Hou can capture an& save &ata a'out each event to a *ile or SQL Server ta'le to analy,e later. 0or e)ample( you can monitor a pro&uction environment to see which store& proce&ures are hamperin# per*ormance 'y e)ecutin# too slowly. >se SQL <ro*iler to monitor only the events in which you are intereste&. I* traces are 'ecomin# too lar#e( you can *ilter them 'ase& on the in*ormation you want( so that only a su'set o* the event &ata is collecte&. !onitorin# too many events a&&s overhea& to the server an& the monitorin# process an& can cause the trace *ile or trace ta'le to #row very lar#e( especially when the monitorin# process ta3es place over a lon# perio& o* time. What is >ser De*ine& 0unctions" >ser5De*ine& 0unctions allow to &e*ine its own +5SQL *unctions that can accept I or more parameters an& return a sin#le scalar &ata value or a ta'le &ata type. What 3in& o* >ser5De*ine& 0unctions can 'e create&" +here are three types o* >ser5De*ine& *unctions in SQL Server 4III an& they are Scalar( Inline +a'le5 @alue& an& !ulti5statement +a'le5value&. Scalar >ser5De*ine& 0unction A Scalar user5&e*ine& *unction returns one o* the scalar &ata types. +e)t( nte)t( ima#e an& timestamp &ata types are not supporte&. +hese are the type o* user5&e*ine& *unctions that most &evelopers are use& to in other pro#rammin# lan#ua#es. Hou pass in I to many parameters an& you #et a return value. http://www.sqlauthority.com

Anda mungkin juga menyukai