Tuesday, December 1, 2015

SQL - Basic Interview Questions



What is Stored Procedure?
Stored procedures are nothing but a stored procedure is a named group of SQL statements that have been previously created and stored in the server database. Stored procedures accept input parameters so that a single procedure can be used over the network by several clients using different input data. And when the procedure is modified, all clients automatically get the new version. Stored procedures reduce network traffic and improve performance. Stored procedures can be used to help ensure the integrity of the database.
e.g.  sp_helpdb, sp_renamedb, sp_depends etc.

What is Trigger?
A trigger is a SQL procedure that initiates an action when an event (INSERT, DELETE or UPDATE) occurs. Triggers are stored in and managed by the DBMS. Triggers are used to maintain the referential integrity of data by changing the data in a systematic fashion. A trigger cannot be called or executed; DBMS automatically fires the trigger as a result of a data modification to the associated table. Triggers can be viewed as similar to stored procedures in that both consist of procedural logic that is stored at the database level. Stored procedures, however, are not event-drive and are not attached to a specific table as triggers are. Stored procedures are explicitly executed by invoking a CALL to the procedure while triggers are implicitly executed. In addition, triggers can also execute stored procedures.

Nested Trigger: A trigger can also contain INSERT, UPDATE and DELETE logic within itself, so when the trigger is fired because of data modification it can also cause another data modification, thereby firing another trigger. A trigger that contains data modification logic within itself is called a nested trigger.

What is View?
A simple view can be thought of as a subset of a table. It can be used for retrieving data, as well as updating or deleting rows. Rows updated or deleted in the view are updated or deleted in the table the view was created with. It should also be noted that as data in the original table changes, so does data in the view, as views are the way to look at part of the original table. The results of using a view are not permanently stored in the database. The data accessed through a view is actually constructed using standard T-SQL select command and can come from one to many different base tables or even other views.

What is Index?
An index is a physical structure containing pointers to the data. Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes; they are just used to speed up queries. Effective indexes are one of the best ways to improve performance in a database application. A table scan happens when there is no index available to help a query. In a table scan SQL Server examines every row in the table to satisfy the query results. Table scans are sometimes unavoidable, but on large tables, scans have a terrific impact on performance.

What is a Linked Server?
Linked Servers is a concept in SQL Server by which we can add other SQL Server to a Group and query both the SQL Server dbs using T-SQL Statements. With a linked server, you can create very clean, easy to follow, SQL statements that allow remote data to be retrieved, joined and combined with local data. Stored Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used add new Linked Server.



What is Cursor?
Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time. In order to work with a cursor we need to perform some steps in the following order:
Declare cursor
Open cursor
Fetch row from the cursor
Process fetched row
Close cursor
Deallocate cursor

Wednesday, September 9, 2015

VBA Vs Power BI, PowerPivot, PowerQuery, PowerView and PowerMap

VBA and the Power tools target different pieces of the pie and different use scenarios. For an analyst I would focus on the Power stuff and using VBA for production needs. 

VBA have been around for almost as long as MS Excel and in all this time VBA has created a space for itself and all its related software over many years, Software like Power BI, Power Pivot etc are all relatively new and can be seen as a high level problem solver where as with VBA, the level goes a lot lower and will most definitely involve role players at that lower level

VBA falls short in that many users are scared of it - the interface is too busy, too many moving parts and the object model takes a long time to get familiar with - it is ot particulary easy to break into VBA coding.

PowerPivot falls short in that the types of operations that it performs are somewhat more limited - the output is always a reconfigured version of what was put into it, with a few fancy calculations and metrics.

Where PowerQuery really shines is that you can take a small amount of data and make it big. Running a full fledged simulation is easy, contorting and expanding and blowing up your data is easy. A user can create reusable and global abstract functions and apply across a workbook. 
Power Query is the new swiss army knife for accountants and controllers in my eyes. Although "only meant" as a self-service ETL-tool, it's so versatile that you can extend it's use cases in so many areas.

Its main drawback compared to VBA - lack of runtime parameters, I/O, user prompts etc. is easily worked around by good design and trial and error. The programming language is relatively easy to pick up and more intuitive than VBA IMHO.

PowerQuery also easily interfaces with PowerPivot directly - it is not a either-or proposition, although I find that given how easy it s to mash-up tables in PowerQuery, I can do most of the heavy lifting that I would have used PowerPivot for directly in PowerQuery. I will concede that if I were better with PowerPivot, I might change my position, but The financial data that I work with and the projects that I have do not require the sort of fancy analysis that PowerPivot measures are better suited for. 
Powerquery is very underrated and under appreciated - much more powerful and useful in everyday tasks than VBA and even basic excel formulas.

The Power BI stuff appears to be on an unannounced track to replace SSRS. It is much better and modern. The new announcements about open source visualizations, mobile, and more will make Power BI even more competitive in that space. Take an xls combining Power BI using Sharepoint 2013+ data source and a few VBA supported easy buttons and you have a powerful tool.

If data cleansing is a big part of your work, PowerQuery should always come before PowerPivot. As a convert from VBA to PowerQuery, A huge drawback to VBA is that VBA is scary to outsiders - if you are not a programmer, you think it is like Pandora's Box; and if you are a programmer, you have likely seen to many excel "experts" that have macro-recorded themselves incredibly non-extendable and difficult to follow scripts that they cant even read their own code for.

PowerQuery is fresh and intuitive. If you are an outsider reading a query that a novice developed exclusively from the UI, you can pick it up and read it and make it your own with minimal effort.

Tuesday, August 25, 2015

Event Notifications vs. SQL Trace

Event Notifications convey the very same data as DDL triggers and occur on the very same events, but they are asynchronous and loosely coupled as SQL Trace. 

To know more on click on Event Notifications vs. Triggers.